In Struts2, HttpServletRequest, HttpSession, and ServletContext are encapsulated and decoupled, and three Map objects are constructed to replace the listed objects.
method of obtaining:
① acquisition (the acquisition only way to get the domain space ServletAPI, and through ActionContext non-real specific ServletAPI )
Put data into the Request domain space: ActionContext.getContext().put("req", "req_value");
Put data into the Session domain space: ActionContext.getContext().getSession().put("ses", "ses_value");
Put data into the Application domain space: ActionContext.getContext().getApplication().put("app", "app_value");
② Get through ServletActionContext ( get the real specific ServletAPI )
Put data into the Request domain space: ServletActionContext.getRequest().setAttribute("req", "req_value");
Put data into the Session domain space: ServletActionContext.getRequest().getSession().setAttribute("ses", "ses_value");
Put data into the Application domain space: ServletActionContext.getServletContext().setAttribute("app", "app_value");
③ Obtain by implementing specific interfaces (action class implements RequestAware, SessionAware, ApplicationAware interfaces)
// 三大成员变量
private Map<String, Object> req;
private Map<String, Object> ses;
private Map<String, Object> app;
// 三大成员属性
public void setRequest(Map<String, Object> request) {
this.req = request;
}
public void setSession(Map<String, Object> session) {
this.ses = session;
}
public void setApplication(Map<String, Object> application) {
this.app = application;
}
public String execute() {
req.put("req", "req_value");
ses.put("ses", "ses_value");
app.put("app", "app_value");
return "success";
}