1.通过ActionContext (推荐)
public class Demo5Action extends ActionSupport { public String execute() throws Exception { //request域=> map (struts2并不推荐使用原生request域) //不推荐 Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request"); //推荐 ActionContext.getContext().put("name", "requestTom"); //session域 => map Map<String, Object> sessionScope = ActionContext.getContext().getSession(); sessionScope.put("name", "sessionTom"); //application域=>map Map<String, Object> applicationScope = ActionContext.getContext().getApplication(); applicationScope.put("name", "applicationTom"); return SUCCESS; } }
2.通过ServletActionContext
public class Demo6Action extends ActionSupport { //并不推荐 public String execute() throws Exception { //原生request HttpServletRequest request = ServletActionContext.getRequest(); //原生session HttpSession session = request.getSession(); //原生response HttpServletResponse response = ServletActionContext.getResponse(); //原生servletContext ServletContext servletContext = ServletActionContext.getServletContext(); return SUCCESS; } }
3.通过实现接口方式
public class Demo7Action extends ActionSupport implements ServletRequestAware { private HttpServletRequest request; public String execute() throws Exception { System.out.println("原生request:"+request); return SUCCESS; } @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } }