在传统Web开发中,经常会用到Servlet API中的HttpServletRequest、HttpSession和ServletContext。Struts2框架让我们可以直接访问和设置action及模型对象的数据,这降低了对HttpServletRequest对象的需求,但在某些应用中,我们可能会需要在action中去访问HttpServletRequest对象及其他两种对象,例如,用户登录成功后,我们要将用户信息保存到Session中。
Struts2提供了两种方式去访问者三种对象,第一种是与Servlet API 解耦的访问方式;第二种是与Servlet API耦合的访问方式。
使用与Servlet API解耦的方式访问:
Struts2对HttpServletRequest、HttpSession、ServletContext进行了封装,构造了三个Map对象来代替这三个对象,在Action中直接使用它们三者各自对应的Map进行操作,那么如何获取这三个Map对象呢?可以使用com.opensymphony.xwork2.ActionContext类。
ActionContext类是action执行的上下文,ActionContext类定义了如下方法,用于获取HttpServletRequest、HttpSession和ServletContext对应的Map对象
public Object get(Object key):ActionContext中没有提供getRequest()方法直接获取封装了HttpServletRequest的Map对象,如果要获取该Map对象必须使用get("request")这样的方式获取HttpServletRequest对应的Map对象。
public Map getSession():获取封装了HttpSession的Map对象。
public Map getApplication():获取封装了ServletContext的Map对象。
测试方法:
public String helloTest(){ /*设置username的值*/ this.setUname("hello:" + this.getUname()); /*获取action上下文对象*/ ActionContext ctx = ActionContext.getContext(); /*通过ctx获取封装了ServletContext对应的Map集合,并存值*/ Map application = ctx.getApplication(); application.put("app", "你好!application!"); /*通过ctx获取封装了HttpSession对应的Map集合,并设置值*/ Map session = ctx.getSession(); session.put("session", "你好!session!"); /*因为ActionContext类没有提供getRequest()方法,如果要得到封装了HttpServletRequest的Map对象需要传参*/ Map request = (Map) ctx.get("request"); request.put("req", "你好! request!"); return "ok"; }
在jsp页面进行接收:
${applicationScope.app} </br> ${sessionScope.session} </br> ${requestScope.req} </br>
注:如果我们想在Action和页面之间单纯的传递数据,我们可以使用ActionContext一步到位:
/*可以使用ActionContext直接传值*/ ActionContext.getContext().put("ctx", "你好!!!");
默认为request范围内可以接到值。
除了上述方法外可以使Action类实现RequestAware、SessionAware、ApplicationAware的方式获取request、session和application等对象(这种方式不提倡,太罗嗦)!
与Servlet API耦合的方式访问:
直接访问Servlet API将使你的Action与Servlet环境耦合在一起,我们知道对于HttpServletRequest、HttpServletResponse和ServletContext这些对象,他们都是由Servlet容器来构造的,有时候,我们的业务需求(比如要通过request获取IP地址)确实需要直接访问这些对象。此时我们就可以通过org.apache.struts2.ServletActionContext类,该类时ActionContext的子类,在这个类中提供了静态方法:
public static HttpServletRequest getRequest():得到HttpServletRequest对象。
public static HttpServletResponse getResponse():获取HttpServletResponse对象。
public static ServletContext getServletContext():得到ServletContext对象。
注:ServleActionContext类中并没有提供获取HttpSession的方法,但是有了request,要获取session是非常简单的。
示例代码如下:
public String helloTest1(){ /*通过ServletActionContext获取HttpServletRequest对象*/ HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("req", "你好,我是request!"); /*通过ServletActionContext获取HttpResponse对象*/ HttpServletResponse response = ServletActionContext.getResponse(); /*通过ServletActionContext获取ServletContext对象*/ ServletContext ctx = ServletActionContext.getServletContext(); ctx.setAttribute("ctx", "你好!我是application!"); /*通过ServletActionContext来获取HttpRequest对象,再获取Session对象*/ HttpServletRequest req = ServletActionContext.getRequest(); HttpSession sesison = req.getSession(); sesison.setAttribute("session", "你好!我是Session~!"); return "ok"; }
除了使用上述方法获取HttpServletRequest和ServletContext对象之外,还可以调用ActionContext对象的get()方法,传递ServletActionContext.HTTP_REQUEST和ServletActionContext_SERVLET_CONTEXT分别来获取HttpServletRequest和ServletContext对象。
示例代码如下:
public String helloTest2(){ /*通过ActionContext获取HttpServletRequest对象*/ HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); request.setAttribute("req", "你好,我是request!"); /*通过ActionContext获取HttpServletResponse对象*/ HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE); /*通过ActionContex获取ServletContext对象*/ ServletContext ctx = (ServletContext) ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT); ctx.setAttribute("ctx", "你好!我是application!!"); /*通过ServletActionContext来获取HttpRequest对象,再获取Session对象*/ HttpServletRequest req = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); HttpSession sesison = req.getSession(); sesison.setAttribute("session", "你好!我是Session!!"); return "ok"; }