标签:text 请求 cat app 保存 work intercept public return
1、直接获取servletapi
核心类:servletActionContext提供的静态方法
package com.data; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; /* * 数据操作 */ import com.opensymphony.xwork2.ActionSupport; public class DataAction extends ActionSupport { @Override public String execute() throws Exception { // TODO Auto-generated method stub //1、请求数据封装,2、调用servlet处理业务,拿到结果数据集 //3、数据保存到域中 //struts对数据的操作,方式一,直接拿到servletApi,执行操作 HttpServletRequest request=ServletActionContext.getRequest(); HttpSession session=request.getSession(); ServletContext application=request.getServletContext(); //操作 request.setAttribute("request_data", "request_data"); request.setAttribute("session_data", "session_data"); request.setAttribute("application_data", "application_data"); return SUCCESS; } }
2、通过ActionContext获取不同的map
(注意:对actionContext的初始化不能再构造方法中执行,因为struts的执行顺序是:先执行Action类的创建,在执行拦截器,拦截器执行完后,在执行action类的业务逻辑方法,会导致数据为空)
package com.data; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; /* * 数据操作 */ import com.opensymphony.xwork2.ActionSupport; public class DataAction1 extends ActionSupport { @Override public String execute() throws Exception { // TODO Auto-generated method stub /* * 解耦的方式实现对数据的操作 */ ActionContext ac=ActionContext.getContext(); //得到struts对HttpServletRequest对象进行封装为一个Map对象 //拿到表示request对象的Map Map<String ,Object> request=ac.getContextMap(); //拿到表示session对象的map Map<String , Object> session=ac.getSession(); //拿到表示ServletContext对象的Map Map<String, Object> application=ac.getApplication(); request.put("request_data", "request_data_ActionContext"); request.put("session_data", "session_data_ActionContext"); request.put("application_data", "application_data_ActionContext"); return SUCCESS; } }
3、实现接口的方法(RequestAware,SessionAware,ApplicationAware)
package com.data; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionContext; /* * 数据操作 */ import com.opensymphony.xwork2.ActionSupport; /* * 方式三:实现接口的方式 */ public class DataAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware { private Map<String, Object> request; private Map<String, Object> session; private Map<String, Object> application; //在struts运行的时候会把代表application的map注入 @Override public void setApplication(Map<String, Object> arg0) { // TODO Auto-generated method stub this.application=arg0; } //在struts运行的时候会把代表session的map注入 @Override public void setSession(Map<String, Object> arg0) { // TODO Auto-generated method stub this.session=arg0; } //在struts运行的时候会把代表request的map注入 @Override public void setRequest(Map<String, Object> arg0) { // TODO Auto-generated method stub this.request=arg0; } @Override public String execute() throws Exception { // TODO Auto-generated method stub //数据 request.put("request_data", "request_data_Aware"); request.put("session_data", "session_data_Aware"); request.put("application_data", "application_data_Aware"); return SUCCESS; } }
总结:
第一种是与servlet的耦合度很高,其余两种都是解耦的方式,第二种和第三种的区别不大,如果action中业务方法很多,最好使用第三种,因为获取的request,session和application几乎都会在业务方法中执行的。
标签:text 请求 cat app 保存 work intercept public return
原文地址:http://www.cnblogs.com/helloworldcode/p/6160418.html