Struts的数据处理方式
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");
方式二:通过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");
方式三:实现接口RequestAware, SessionAware, ApplicationAware
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对象注入@Overridepublic void setRequest(Map<String, Object> request) {this.request = request;}// 注入session@Overridepublic void setSession(Map<String, Object> session) {this.session = session;}// 注入application@Overridepublic void setApplication(Map<String, Object> application) {this.application = application;} @Overridepublic 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;} }
注意:在非必要使用方式一的情况下,建议使用方式二或者方式三(因为方式二、方式三有很多方式一功能无法实现)(当类中有多个业务方法,建议使用方式二)
新闻热点
疑难解答