在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=" ...
随机推荐
- 0108MySQL集群搭建详解(三种结点分离)
转自http://blog.csdn.net/yang1982_0907/article/details/20716845,感谢博主 本文将搭建一个最简化的MySQL Cluster系统,配置方法中的 ...
- C#中的Attribute Property区别
Attribute 一般译作"特性",Property 仍然译为"属性". Attribute 是一种可由用户自由定义的修饰符(Modifier),可以用来修饰 ...
- ASP.NET学习笔记01
ASP.NET初级工程师的核心要求:能够实现一个基本的网站. ASP.NET初级工程师面试主要要求: 1.基础的数据结构和算法 2.C#编程语言基础 3.网站基础(HTML,CSS,Javascrip ...
- <LeetCode OJ> 100. Same Tree
100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...
- Codeforces Gym 100015F Fighting for Triangles 状态压缩DP
F Fighting for Triangles Description Andy and Ralph are playing a two-player game on a triangular bo ...
- 非Qt工程使用Qt的信号槽机制
非Qt工程,使用Qt的信号槽机制,蛋疼不?反正我现在就是要做这样一件蛋疼的事. 要使用Qt的信号槽机制,下面是从Qt Assist里面关于 signal & slots 的一句介绍: All ...
- Map (就一个json.jar)
public static void main(String[] args) { List<Map<Integer, String>> m = new ArrayList< ...
- redis 五大数据类型的常用指令
STRING 192.168.1.66:6379> get k1 "v1" 192.168.1.66:6379> append k1 12345 (integer) 7 ...
- PHPStorm中使用bootstrap3控件!
PHPStorm中使用bootstrap3控件! 奇怪为什么不自动提示呢? 原来需要Ctrl+j才显示出来! 很方便的控件!!!!
- go并发设计模式 --资源生成器模式
1.简单的生成器 package main import ( "fmt" "math/rand" ) func GenerateIntA()chan int { ...