Struts2的Action组件是不依赖servlet API 的。那么当你在action中的业务需要处理HttpServletRequest和HttpServletResponse的时候(比如要对响应做处理写cookie,生成验证码)怎么办呢?

有3种办法可以实现action中获取servlet api

1.使用ServletActionContext的静态方法

 

Struts2使用ServletActionContext对象维护Servlet api 对象(像request,response,session,application)。ServletActionContext使用ThreadLocal(线程局部变量,关于ThreadLocal请参看本博另一篇文章《理解TheadLocal(线程局部变量)》),这样能保证获取的是当前用户当前线程的servlet api对象。

  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

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

在Action中获取servlet API的更多相关文章

  1. [技巧篇]08.Struts2拦截器中获取Servlet API方法

    讲课中遇到的解决Session拦截器的后腿问题,还有如何在拦截器中获取Servlet API,这里留一个备注,方便学生查找

  2. Struts2中获取servlet API的几种方式

    struts2是一个全新的MVC框架,如今被广大的企业和开发者所使用,它的功能非常强大.这给我们在使用servlet 纯java代码写项目的时候带来了福音.但是一般来说,我们的项目不到一定规模并不需要 ...

  3. struts2中访问servlet API

    Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...

  4. struts2:在Action中使用Servlet的API,设置、读取各种内置对象的属性

    有两种方式可以实现在Action中使用Servlet的API.一种是使用org.apache.struts2.ServletActionContext类,另一种是使用com.opensymphony. ...

  5. 9.Struts2在Action中获取request-session-application对象

    为避免与Servlet API耦合在一起,方便Action类做单元测试. Struts2对HttpServletRequest.HttpSession.ServletContext进行了封装,构造了三 ...

  6. Struts2中使用Servlet API步骤

    Struts2中使用Servlet API步骤 Action类中声明request等对象 Map<String, Object> request; 获得ActionContext实例 Ac ...

  7. 在Action中获取表单提交数据

    -----------------siwuxie095 在 Action 中获取表单提交数据 1.之前的 Web 阶段是提交表单到 Servlet,在其中使用 Request 对象 的方法获取数据 2 ...

  8. java:struts框架2(方法的动态和静态调用,获取Servlet API三种方式(推荐IOC(控制反转)),拦截器,静态代理和动态代理(Spring AOP))

    1.方法的静态和动态调用: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...

  9. struts2:JSP页面及Action中获取HTTP参数(parameter)的几种方式

    本文演示了JSP中获取HTTP参数的几种方式,还有action中获取HTTP参数的几种方式. 1. 创建JSP页面(testParam.jsp) <%@ page language=" ...

随机推荐

  1. 2015 Multi-University Training Contest 10 hdu 5407 CRB and Candies

    CRB and Candies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  2. 线程锁的机制Lock

    java.util.concurrent.locks 接口Lock publci interface Lock Lock 实现提供了比使用synchronized方法和语句可获得的更加广泛的锁定操作, ...

  3. nodejs-mysql模块

    安装mysql模块 1 npm install -g mysql node中使用Mysql模块来执行mysql命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 var ht ...

  4. 非阻塞IO函数

    关于效率的优化:

  5. C#高级编程八十三天----程序集的含义

    程序集的含义 一.程序集是包括一个或多个类型定义文件和资源文件的集合.它同意我们分析可重用类型的逻辑表示和物理表示. 相当于你定义了一个项目XXProject,项目存在非常多文件(类,窗口,接口,资源 ...

  6. 兔子-RadioButton和RadioGroup的关系

    RadioButton和RadioGroup的关系: 1.RadioButton表示单个圆形单选框.而RadioGroup是能够容纳多个RadioButton的容器 2.每一个RadioGroup中的 ...

  7. 可编程数据平面将OpenFlow扩展至电信级应用(二)

    可编程数据平面将OpenFlow扩展至电信级应用(二) 案例:基于WinPath网络处理器的电信极OpenFlow (CG-OF)client实现 作者:Liviu Pinchas, Tao Lang ...

  8. Linux程序设计学习笔记——异步信号处理机制

    转载请注明出处: http://blog.csdn.net/suool/article/details/38453333 Linux常见信号与处理 基本概念 Linux的信号是一种进程间异步的通信机制 ...

  9. 【Cocos2d-x】坐标系和图层

    在Cocos2D-X中,存在四种坐标系: 1.OpenGL坐标系:该坐标系原点在屏幕左下角.x轴向右,y轴向上.这也就是cocos2dx中用到的坐标系所以没啥好说的. 2.屏幕坐标系(UIKit坐标) ...

  10. 数组进行多少次OP操作,才干有序

    1 题目描写叙述: 有一个数组:2,1,4,3.对于数组,有一种操作op(idx):将该index相应的数字移到首位.比方: op(3):  2 1 43 -> 3 2 1 4 op(1):   ...