Struts2 访问获取request,session和application对象



Struts2 访问获取request,session和application对象。

与Servlet API解耦的访问方式

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”

 

  1. public Object get(Object key)
  2. public Map getSession()
  3. public Map getApplication()
public Object get(Object key)

public Map getSession()

public Map getApplication()

Action:

 

  1. ActionContext context = ActionContext.getContext();
  2. Map request=(Map)context.get(“request”);
  3. Map session = context.getSession();
  4. Map application = context.getApplication();
  5. request.put(“greeting” “hello request”);
  6. session.put(“user”,user);
  7. Integer count = (Integer)application.get(“counter”);
  8. application.put(“counter”,count);
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);

JSP:

 

 

  1. <h3>${sessionScope.user.username},${requestScope.greeting}。</h3><br />
  2. ${applicationScope.counter}
<h3>${sessionScope.user.username},${requestScope.greeting}。</h3><br />
${applicationScope.counter}

利用请求对象来传递数据还有一种方式,可以直接利用ActionContext类的put()方法将数据保存到ActionContext中

 

 

  1. Actioncontext.getContext().put(“greeting”,”hello “);
Actioncontext.getContext().put("greeting","hello ");

然后在结果页面中,从请求对象中取出greeting属性

 

 

  1. $(requestScope.greeting)或者<%=request.getAttribute(“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对象。与之对应的三个接口和他们的方法:

 

  1. org.apache.struts2.interceptor.RequestAware
  2. org.apache.struts2.interceptor.SessionAware
  3. org.apache.struts2.interceptor.ApplicationAware
org.apache.struts2.interceptor.RequestAware
org.apache.struts2.interceptor.SessionAware
org.apache.struts2.interceptor.ApplicationAware
  1. public class LoginAction implements Action,RequestAware,SessionAware,ApplicationAware{
  2. private Map request;
  3. private Map session;
  4. private Map application;
  5. // ……
  6. @override
  7. public void setRequest(Map request){
  8. this.request=request;
  9. }
  10. @override
  11. public void setSession(Map session){
  12. this.session=session;
  13. }
  14. @override
  15. public void setApplication(Map application){
  16. this.application=application;
  17. }
  18. }
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;
}

}

 

与Servlet API耦合的访问方式

要直接获取HttpServletRequest和ServletContext对象,可以使用org.apache.struts2.ServletActionContext类,在这个类中,定义了两个静态方法

 

  1. public static HttpserlvetRequest getRequest()
  2. public static HttpservletResponse getResponse()
  3. pubolic static ServletContext getServletContext()
public static HttpserlvetRequest getRequest()

public static HttpservletResponse getResponse()

pubolic static ServletContext getServletContext()

HttpSession对象可以通过HttpServletRequest对象来取到

 

还可以调用ActionContext对象的get()方法,传递ServletActionContext.HTTP_REQUEST和ServletActionContext.SERVLET_CONTEXT键值来得到HttpServletRequest和ServletContext对象,如下:

 

  1. ActionContext.getcontext().get(ServletActionContext.HTTP_REQUEST);
  2. ActionContext.getcontext().get(ServletActionContext.HTTP_RESPONSE);
  3. ActionContext.getcontext().get(ServletActionContext.SERVLET_CONTEXT);
ActionContext.getcontext().get(ServletActionContext.HTTP_REQUEST);
ActionContext.getcontext().get(ServletActionContext.HTTP_RESPONSE);
ActionContext.getcontext().get(ServletActionContext.SERVLET_CONTEXT);
  1. HttpServletResquest request  = ServletActionContext.getRequest();
  2. HttpSession session = request.getSession();
  3. ServletContext context = ServletActionContext.getServletContext();
  4. //application中保存数据
  5. Integer count = (Integer)context.getAttribute(“counter”);
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包中。

 

  1. public class LoginAction implements Action,ServletRequestAware,ServletContextAware{
  2. private HttpServletRequest request;
  3. private ServletContext context;
  4. //……
  5. @override
  6. public void setServletRequest(…){
  7. //…
  8. }
  9. @override
  10. public void setServletContext(…){
  11. //…
  12. }
  13. }
  14. http://blog.csdn.net/dawnming/article/details/6916707