Structs2对HttpServletRequest,HttpSession,和ServletContext进行了封装,构造了三个Map对象来替代这三种对象,在Action中,直接使用HttpServletRequest,Httpsession,ServletContext对应的Map对象来保存和读取数据。
要获取这三个Map对象,可以使用com.opensymphony.xwork2.ActionContext类。
ActionContext是action执行的上下文,在ActionContext中保存了action执行所需要的一组对象,包括parameters,request,session,application,locale等。
ActionContext类没有提供类似getRequest()这样的方法来获取封装了HttpServletRequest的Map对象。要请求Map对象,需要用get()方法传递参数"request"
public Object get(Object key) public Map getSession() public Map getApplication()
Action:
ActionContext context = ActionContext.getContext(); Map request=(Map)context.get("request"); Map session = context.getSession(); Map application = context.getApplication(); request.put("greeting" "hello request"); session.put("user",user); Integer count = (Integer)application.get("counter"); application.put("counter",count);
<h3>${sessionScope.user.username},${requestScope.greeting}。</h3><br /> ${applicationScope.counter}
Actioncontext.getContext().put("greeting","hello ");然后在结果页面中,从请求对象中取出greeting属性
$(requestScope.greeting)或者<%=request.getAttribute("greeting")%>ActionContext中保存的数据能够从请求对象中得到,其中的奥妙就在于Struct2中的org.apache.struts2.dispatcher.StrutsRequestWrapper类,这个类是HttpServletRequest的包装类,它重写了getAttribute()方法,在这个方法中,先请求对象中查找属性,如果没有,就从ActionContext中查找。这就是为什么ActionContext中保存的数据能够从请求对象中得到的原因。
除了ActionContext来获取request,session和application对象这种方式外,Action类还可以实现某些特定接口,让Structs2在运行时向Action实例注入request,session,application对象。与之对应的三个接口和他们的方法:
org.apache.struts2.interceptor.RequestAware org.apache.struts2.interceptor.SessionAware org.apache.struts2.interceptor.ApplicationAware
public class LoginAction implements Action,RequestAware,SessionAware,ApplicationAware{ private Map request; private Map session; private Map application; // ...... @override public void setRequest(Map request){ this.request=request; } @override public void setSession(Map session){ this.session=session; } @override public void setApplication(Map application){ this.application=application; } }
要直接获取HttpServletRequest和ServletContext对象,可以使用org.apache.struts2.ServletActionContext类,在这个类中,定义了两个静态方法
public static HttpserlvetRequest getRequest() public static HttpservletResponse getResponse() pubolic static ServletContext getServletContext()HttpSession对象可以通过HttpServletRequest对象来取到
还可以调用ActionContext对象的get()方法,传递ServletActionContext.HTTP_REQUEST和ServletActionContext.SERVLET_CONTEXT键值来得到HttpServletRequest和ServletContext对象,如下:
ActionContext.getcontext().get(ServletActionContext.HTTP_REQUEST); ActionContext.getcontext().get(ServletActionContext.HTTP_RESPONSE); ActionContext.getcontext().get(ServletActionContext.SERVLET_CONTEXT);
HttpServletResquest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); ServletContext context = ServletActionContext.getServletContext(); //application中保存数据 Integer count = (Integer)context.getAttribute("counter");
除了利用ServletActionContext来获取HttpServletRequest对象和ServletContext对象这种方式外,Action类还可以实现ServletRequestAware和ServletContextAware接口,由Struts2向Action实例注入HttpServletRequest和ServletContext对象
ServeletRequestAware接口和ServletContextAware接口不在同一个包,前者在org.apache.structs2.interceptor包中,后者在org.apache.struts2.util包中。
public class LoginAction implements Action,ServletRequestAware,ServletContextAware{ private HttpServletRequest request; private ServletContext context; //...... @override public void setServletRequest(...){ //... } @override public void setServletContext(...){ //... } }