在Action中获取servlet API
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对象。
- public class TestAction {
- HttpServletRequest request = ServletActionContext.getRequest();
- HttpServletResponse response = ServletActionContext.getResponse();
- HttpSession session = request.getSession();
- ActionContext actionContext = ServletActionContext.getActionContext(request);
- ActionContext context = ServletActionContext.getContext();
- ActionMapping mapping = ServletActionContext.getActionMapping();
- PageContext pageContext = ServletActionContext.getPageContext();
- ServletContext servletContext = ServletActionContext.getServletContext();
- ValueStack valueStack = ServletActionContext.getValueStack(request);
- }
2.使用ActionContext
- public class TestAction {
- ActionContext context = ActionContext.getContext();
- public void test(){
- ActionInvocation actionInvocation = context.getActionInvocation();
- Locale locale = context.getLocale();
- ValueStack valueStack = context.getValueStack();
- Container container = context.getContainer();
- Map<String, Object> parameters = context.getParameters();
- Map<String, Object> session = context.getSession();
- Map<String, Object> application = context.getApplication();
- Map<String, Object> contextMpap = context.getContextMap();
- Map<String, Object> conversionErrorss = context.getConversionErrors();
- }
- }
在Action中获取servlet API的更多相关文章
- [技巧篇]08.Struts2拦截器中获取Servlet API方法
讲课中遇到的解决Session拦截器的后腿问题,还有如何在拦截器中获取Servlet API,这里留一个备注,方便学生查找
- Struts2中获取servlet API的几种方式
struts2是一个全新的MVC框架,如今被广大的企业和开发者所使用,它的功能非常强大.这给我们在使用servlet 纯java代码写项目的时候带来了福音.但是一般来说,我们的项目不到一定规模并不需要 ...
- struts2中访问servlet API
Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...
- struts2:在Action中使用Servlet的API,设置、读取各种内置对象的属性
有两种方式可以实现在Action中使用Servlet的API.一种是使用org.apache.struts2.ServletActionContext类,另一种是使用com.opensymphony. ...
- 9.Struts2在Action中获取request-session-application对象
为避免与Servlet API耦合在一起,方便Action类做单元测试. Struts2对HttpServletRequest.HttpSession.ServletContext进行了封装,构造了三 ...
- Struts2中使用Servlet API步骤
Struts2中使用Servlet API步骤 Action类中声明request等对象 Map<String, Object> request; 获得ActionContext实例 Ac ...
- 在Action中获取表单提交数据
-----------------siwuxie095 在 Action 中获取表单提交数据 1.之前的 Web 阶段是提交表单到 Servlet,在其中使用 Request 对象 的方法获取数据 2 ...
- java:struts框架2(方法的动态和静态调用,获取Servlet API三种方式(推荐IOC(控制反转)),拦截器,静态代理和动态代理(Spring AOP))
1.方法的静态和动态调用: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...
- struts2:JSP页面及Action中获取HTTP参数(parameter)的几种方式
本文演示了JSP中获取HTTP参数的几种方式,还有action中获取HTTP参数的几种方式. 1. 创建JSP页面(testParam.jsp) <%@ page language=" ...
随机推荐
- 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 ...
- 线程锁的机制Lock
java.util.concurrent.locks 接口Lock publci interface Lock Lock 实现提供了比使用synchronized方法和语句可获得的更加广泛的锁定操作, ...
- 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 ...
- 非阻塞IO函数
关于效率的优化:
- C#高级编程八十三天----程序集的含义
程序集的含义 一.程序集是包括一个或多个类型定义文件和资源文件的集合.它同意我们分析可重用类型的逻辑表示和物理表示. 相当于你定义了一个项目XXProject,项目存在非常多文件(类,窗口,接口,资源 ...
- 兔子-RadioButton和RadioGroup的关系
RadioButton和RadioGroup的关系: 1.RadioButton表示单个圆形单选框.而RadioGroup是能够容纳多个RadioButton的容器 2.每一个RadioGroup中的 ...
- 可编程数据平面将OpenFlow扩展至电信级应用(二)
可编程数据平面将OpenFlow扩展至电信级应用(二) 案例:基于WinPath网络处理器的电信极OpenFlow (CG-OF)client实现 作者:Liviu Pinchas, Tao Lang ...
- Linux程序设计学习笔记——异步信号处理机制
转载请注明出处: http://blog.csdn.net/suool/article/details/38453333 Linux常见信号与处理 基本概念 Linux的信号是一种进程间异步的通信机制 ...
- 【Cocos2d-x】坐标系和图层
在Cocos2D-X中,存在四种坐标系: 1.OpenGL坐标系:该坐标系原点在屏幕左下角.x轴向右,y轴向上.这也就是cocos2dx中用到的坐标系所以没啥好说的. 2.屏幕坐标系(UIKit坐标) ...
- 数组进行多少次OP操作,才干有序
1 题目描写叙述: 有一个数组:2,1,4,3.对于数组,有一种操作op(idx):将该index相应的数字移到首位.比方: op(3): 2 1 43 -> 3 2 1 4 op(1): ...