package com.loaderman.c_data; import java.util.Map; import javax.servlet.ServletContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 数据处理 * */ public class DataAction_bak extends ActionSupport{ @Override public String execute() throws Exception { // 1. 请求数据封装; 2. 调用Service处理业务逻辑,拿到结果数据 // 3. 数据保存到域中 /* * // Struts中对数据操作,方式1: 直接拿到ServletApi, 执行操作 HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); ServletContext application = ServletActionContext.getServletContext(); // 操作 request.setAttribute("request_data", "request_data1"); session.setAttribute("session_data", "session_data1"); application.setAttribute("application_data", "application_data1"); */ // 【推荐:解耦的方式实现对数据的操作】 // Struts中对数据操作,方式2: 通过ActionContext类 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_data1_actionContext"); session.put("session_data", "session_data1_actionContext"); application.put("application_data", "application_data1_actionContext"); return SUCCESS; } }
package com.loaderman.c_data; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import java.util.Map; /** * 数据处理, 方式3: 实现接口的方法 */ public class DataAction extends ActionSupport implements RequestAware, SessionAware, ApplicationAware { private Map<String, Object> request; private Map<String, Object> session; private Map<String, Object> application; // struts运行时候,会把代表request的map对象注入 @Override public void setRequest(Map<String, Object> request) { this.request = request; } // 注入session @Override public void setSession(Map<String, Object> session) { this.session = session; } // 注入application @Override public void setApplication(Map<String, Object> application) { this.application = application; } @Override public String execute() throws Exception { // 数据 request.put("request_data", "request_data1_actionAware"); session.put("session_data", "session_data1_actionAware"); application.put("application_data", "application_data1_actionAware"); return SUCCESS; } }