action中访问request session application
前两种是取得Map类型的request,session,application 后两种是采用真实类型HttpServletReqeust,HttpSession,ServletContext
第一种方法依赖于容器 采用ActionContext即上下文的方式获取。jsp页面中采用原始的获取方式和<s:property value="#request.r1" />标签获取
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.ouc.wkp.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction1 extends ActionSupport{ private Map request; private Map session; private Map application; public LoginAction1(){ //context 上下文 程序执行时周围的环境 request=(Map) ActionContext.getContext().get("request"); session=ActionContext.getContext().getSession(); application=ActionContext.getContext().getApplication(); } public String execute(){ request.put("r1", "r1"); session.put("s1", "s1"); application.put("a1", "a1"); return SUCCESS; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<body>
User Login Success
<s:debug></s:debug>
<s:property value="#request.r1" /> \\ <%=request.getAttribute("r1") %>
<s:property value="#session.s1" /> \\ <%=session.getAttribute("s1") %>
<s:property value="#application.a1"/>\\<%=application.getAttribute("a1") %>
<s:property value="#attr.a1"/>
</body>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<form name="f" action="" method="post"> <input type="text" name="username" /> <input type="text" name="password" /> <input type="button" value="submit1" onclick="javascript:document.f.action='login/login1';document.f.submit()" /> </form>
<s:debug></s:debug>可以查看到很多信息 程序中的request会和Map型的request结合在一起
第二种方法ioc
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.ouc.wkp.action; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{ //设计模式 //DI dependency injection //IOC inverse of control //Map和HttpRequest之间做了映射 //依赖别人注入一个值 private Map<String, Object> request; private Map<String, Object> session; private Map<String, Object> application; public String execute(){ request.put("r1", "r2"); session.put("s1", "s2"); application.put("a1", "a2"); return SUCCESS; } @Override public void setApplication(Map<String, Object> application) { this.application=application; } @Override public void setSession(Map<String, Object> session) { this.session=session; } @Override public void setRequest(Map<String, Object> request) { this.request=request; } }
第三种方法依赖于容器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.ouc.wkp.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction3 extends ActionSupport{ private HttpServletRequest request; private HttpSession session; private ServletContext application; public LoginAction3(){ request=ServletActionContext.getRequest(); session=request.getSession(); application=request.getServletContext(); } public String execute(){ request.setAttribute("r1", "r3"); session.setAttribute("s1", "s3"); application.setAttribute("a1", "a3"); return SUCCESS; } }
第四种方法ioc
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.ouc.wkp.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; public class LoginAction4 extends ActionSupport implements ServletRequestAware{ private HttpServletRequest request; private HttpSession session; private ServletContext application; @Override public void setServletRequest(HttpServletRequest arg0) { request=ServletActionContext.getRequest(); session=request.getSession(); application=request.getServletContext(); } public String execute(){ request.setAttribute("r1", "r4"); session.setAttribute("s1", "s4"); application.setAttribute("a1", "a4"); return SUCCESS; } }
其中第二种方法最常用