struts servlet API

来源:互联网 发布:互联网金融 知乎 编辑:程序博客网 时间:2024/05/19 07:11

1.使用ServletActionContext的静态方法

 



[java] view plaincopyprint?
  1. public class TestAction {  
  2. HttpServletRequest request = ServletActionContext.getRequest();  
  3. HttpServletResponse response = ServletActionContext.getResponse();  
  4. HttpSession session =  request.getSession();  
  5. ActionContext actionContext = ServletActionContext.getActionContext(request);  
  6. ActionContext context = ServletActionContext.getContext();  
  7. ActionMapping mapping = ServletActionContext.getActionMapping();  
  8. PageContext pageContext =  ServletActionContext.getPageContext();  
  9. ServletContext servletContext = ServletActionContext.getServletContext();  
  10. ValueStack valueStack = ServletActionContext.getValueStack(request);  
  11. }  

2.使用ActionContext

[java] view plaincopyprint?
  1. public class TestAction {  
  2.     ActionContext context = ActionContext.getContext();  
  3.       
  4.     public void test(){  
  5. ActionInvocation actionInvocation = context.getActionInvocation();  
  6. Locale locale = context.getLocale();  
  7. ValueStack valueStack = context.getValueStack();  
  8. Container container =  context.getContainer();  
  9. Map<String, Object> parameters = context.getParameters();  
  10. Map<String, Object> session = context.getSession();  
  11. Map<String, Object> application = context.getApplication();  
  12. Map<String, Object> contextMpap = context.getContextMap();  
  13. Map<String, Object> conversionErrorss = context.getConversionErrors();  
  14.     }  
  15.       
  16. }  

 


3.使用Struts2的资源注入拦截器


让你的action继承几个接口,实现他们的方法,然后声明serlvet api对象的变量就行了,在运行时struts2会把相对的对象注入到你的变量中。

这种注入不用多解释肯定是拦截器实现的啦!

而且值得注意的是session的接收变量是一个map而不是httpsession


public class TestAction implements ServletContextAware,ServletRequestAware,ServletResponseAware,SessionAware {  

  1. private ServletContext servletContext;  
  2. private HttpServletRequest request;  
  3. private HttpServletResponse response;  
  4. private Map<String, Object> session;  
  5. /***实现相应接口的方法******/  
  6. public void setServletContext(ServletContext arg0) {  
  7. this.servletContext  = arg0;  
  8. }  
  9. public void setServletRequest(HttpServletRequest arg0) {  
  10. This.request = arg0;  
  11. }  
  12. public void setServletResponse(HttpServletResponse arg0) {  
  13. This.response = arg0;  
  14. }  
  15. public void setSession(Map<String, Object> arg0) {  
  16. This.sessoin = arg0;  
  17. }  
  18. }  
原创粉丝点击