訪問servletAPI方式
阿新 • • 發佈:2017-07-27
clas except sets throw map scope con blog 接口
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(); //原生sessionHttpSession 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; } }
訪問servletAPI方式