ActionContextCleanUp
ActionContextCleanUp作用
延长action中属性的生命周期,包括自定义属性,以便在jsp页面中进行访问,让actionContextcleanup过滤器来清除属性,不让action自己清除。
为了使用WebWork,我们只需要在web.xml配置FilterDispatcher一个过滤器即可,阅读一下FilterDispatcher的JavaDoc和源码,我们可以看到它调用了:
finally
{
ActionContextCleanUp.cleanUp(req);
}
在ActionContextCleanUp中,有这样的代码:
req.setAttribute(CLEANUP_PRESENT, Boolean.TRUE);
如果FilterDispatcher检测到这个属性,就不会清除ActionContext中的内容了,而由ActionContextCleanUp后续的代码来清除,保证了一系列的Filter访问正确的ActionContext.
文档中提到,如果用到SiteMesh的Filter或者其他类似Filter,那么设置顺序是:
ActionContextCleanUp filter
SiteMesh filter
FilterDispatcher
所以最后我们的web.xml应该类似这样:
<filter>
<filter-name>ActionContextCleanUp</filter-name>
<filter-class>com.opensymphony.webwork.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.webwork.sitemesh.FreeMarkerPageFilter</filter-class>
</filter>
<filter>
<filter-name>webwork</filter-name>
<filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>ActionContextCleanUp</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
---------------------------------------------------------------------------------------------------------------------
在Struts 2.1.6之前,ActionContextCleanUp的完整路径是com.opensymphony.webwork.dispatcher.ActionContextCleanUp,现在的路径变成了org.apache.struts2.dispatcher.ActionContextCleanup。
那么这个类究竟有什么用处呢?是不是一定要用呢?
下面是这个类内部的注释。
Special filter designed to work with the FilterDispatcher and allow
for easier integration with SiteMesh. Normally, ordering your filters to have
SiteMesh go first, and then FilterDispatcher go second is perfectly fine.
However, sometimes you may wish to access Struts features, including the
value stack, from within your SiteMesh decorators. Because FilterDispatcher
cleans up the ActionContext, your decorator won't have access to the
data you want.
By adding this filter, the FilterDispatcher will know to not clean up and
instead defer cleanup to this filter. The ordering of the filters should then be:
1.this filter
2.SiteMesh filter
3.FilterDispatcher
就是说,一般情况下,如果你要用SiteMesh或者其他过滤器,一般是放在FilterDispatcher或者是现在的StrutsPrepareAndExecuteFilter之前。在调用完所有过滤器的doFilter方法后,核心过滤器FilterDispatcher或者StrutsPrepareAndExecuteFilter会清空ActionContext,如果其他过滤器要一直使用value stack等struts的特性时,如果不用ActionContextCleanUp的话,便得不到想要的值。
ActionContextCleanUp 的作用就是上面用粗体标注出来的那一句。它会在doFilter方法里设置一个计数器counter的初始值为1,有了这个值,后续的核心过滤器就不会清 空ActionContext,而是由之前的过滤器也就是ActionContextCleanUp来清空ActionContext。
1. ActionContext
ActionContext是被存放在当前线程中的,获取ActionContext也是从ThreadLocal中获取的。所以在执行拦截器、 action和result的过程中,由于他们都是在一个线程中按照顺序执行的,所以可以可以在任意时候在ThreadLocal中获取 ActionContext。
ActionContext包括了很多信息,比如Session、Application、Request、Locale、ValueStack等,其中 ValueStack可以解析ognl表达式,来动态获取一些值,同时可以给表达式提供对象。
ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看 作是一个容器 (其实我们这里的容器就是一个Map而已),它存放的是Action在执行时需要用到的对象. 一般情况, 我们的ActionContext都是通过: ActionContext context = (ActionContext) actionContext.get(); 来获取的.我们再来看看这里的actionContext对象的创建:
static ThreadLocal actionContext = new ActionContextThreadLocal();
ActionContextThreadLocal是实现ThreadLocal的一个内部类.ThreadLocal可以命名为"线程局部变量",它为 每一个使用该变量的线程都提供一个变量值的副本,使每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突.这样,我们 ActionContext里的属性只会在对应的当前请求线程中可见,从而保证它是线程安全的.
通过ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession(); (通过Map模拟HttpServlet的对象,操作更方便)
2. ServletActionContext
ServletActionContext(com.opensymphony.webwork. ServletActionContext),这个类直接继承了我们上面介绍的ActionContext,它提供了直接与Servlet相关对象访问的 功能,它可以取得的对象有:
(1)javax.servlet.http.HttpServletRequest : HTTPservlet请求对象
(2)javax.servlet.http.HttpServletResponse : HTTPservlet相应对象
(3)javax.servlet.ServletContext : Servlet上下文信息
(4)javax.servlet.ServletConfig : Servlet配置对象
(5)javax.servlet.jsp.PageContext : Http页面上下文
如何从ServletActionContext里取得Servlet的相关对象:
<1>取得HttpServletRequest对象: HttpServletRequest request = ServletActionContext. getRequest();
<2>取得HttpSession对象: HttpSession session = ServletActionContext. getRequest().getSession();
3. ServletActionContext和ActionContext联系
ServletActionContext和ActionContext有着一些重复的功能,在我们的Action中,该如何去抉择呢?我们遵循的原则 是:如果ActionContext能够实现我们的功能,那最好就不要使用ServletActionContext,让我们的Action尽量不要直接 去访问Servlet的相关对象.
注意:在使用ActionContext时有一点要注意: 不要在Action的构造函数里使用ActionContext.getContext(), 因为这个时候ActionContext里的一些值也许没有设置,这时通过ActionContext取得的值也许是null;同 样,HttpServletRequest req = ServletActionContext.getRequest()也不要放在构造函数中,也不要直接将req作为类变量给其赋值。 至于原因,我想是因为前面讲到的static ThreadLocal actionContext = new ActionContextThreadLocal(),从这里我们可以看出ActionContext是线程安全的,而 ServletActionContext继承自ActionContext,所以ServletActionContext也线程安全,线程安全要求每 个线程都独立进行,所以req的创建也要求独立进行,所以ServletActionContext.getRequest()这句话不要放在构造函数 中,也不要直接放在类中,而应该放在每个具体的方法体中(eg:login()、queryAll()、insert()等),这样才能保证每次产生对象 时独立的建立了一个req。
4.ActionContextClearUp
ActionContextClearUp其实是Defer ClearUP.作用就是延长action中属性的生命周期,包括自定义属性,以便在jsp页面中进行访问,让actionContextcleanup 过滤器来清除属性,不让action自己清除。具体看下面的代码,代码很简单:
- public void doFilter(...){
- ...
- try{
- ...
- //继续执行所配置的chain中的Filter
- chain.doFilter(request, response);
- }finally{
- //保证在所有动作执行完之后,调用cleanUp
- ...
- cleanUp(request);
- }
- }
- protected static void cleanUp(ServletRequest req) {
- ...
- ActionContext.setContext(null);//清除ActionContext实例
- Dispatcher.setInstance(null);//清除Dispatcher实例(Dispatcher主要是完成将url解析成对应的Action)
- }
- public void doFilter(...){
- ...
- try{
- ...
- //继续执行所配置的chain中的Filter
- chain.doFilter(request, response);
- }finally{
- //保证在所有动作执行完之后,调用cleanUp
- ...
- cleanUp(request);
- }
- }
- protected static void cleanUp(ServletRequest req) {
- ...
- ActionContext.setContext(null);//清除ActionContext实例
- Dispatcher.setInstance(null);//清除Dispatcher实例(Dispatcher主要是完成将url解析成对应的Action)
- }
另外注明一下UtilTimerStack的push和pop是用来计算调用方法所执行的开始和结束时间,用来做性能测试的。用法如下:
- String timerKey = "ActionContextCleanUp_doFilter: ";
- UtilTimerStack.setActive(true);
- UtilTimerStack.push(timerKey);
- //调用要测试的方法。
- UtilTimerStack.pop(timerKey);
- String timerKey = "ActionContextCleanUp_doFilter: ";
- UtilTimerStack.setActive(true);
- UtilTimerStack.push(timerKey);
- //调用要测试的方法。
- UtilTimerStack.pop(timerKey);
ActionContextCleanUp的更多相关文章
- Struts2版本更新报错:>>> ActionContextCleanUp <<< is deprecated! Please use the new filters!
因低版本Struts2存在漏洞,更新为较新的版本.启动时,报如下警告信息: ************************************************************** ...
- ActionContextCleanUp作用
延长action中属性的生命周期,包括自定义属性,以便在jsp页面中进行访问,让actionContextcleanup过滤器来清除属性,不让action自己清除. 为了使用WebWork,我们只需要 ...
- struts2升级报ActionContextCleanUp<<is deprecated。Please use the new filters
把web.xml中配置struts.xml的文件改成 <?xml version="1.0" encoding="UTF-8"?> <web- ...
- 菜鸟学Struts2——Struts工作原理
在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...
- 深入浅出Struts2+Spring+Hibernate框架
一.深入浅出Struts2 什么是Struts2? struts2是一种基于MVC的轻量级的WEB应用框架.有了这个框架我们就可以在这个框架的基础上做起,这样就大大的提高了我们的开发效率和质量,为公司 ...
- Struts2+Spring+Hibernate框架整合总结详细教程
一.SSH三大框架知识总结 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与S ...
- struts2工作流程
struts2的框架结构图 工作流程 1.客户端请求一个HttpServletRequest的请求,如在浏览器中输入http://localhost: 8080/bookcode/Reg.action ...
- filter 过滤器(监听)
Filter 过滤器 1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, ...
- Struts2之HelloWorld
首先既然是开发Struts程序的话,那么自然需要用到Struts2开发包,Struts2是apache旗下的开源框架,所有的开发包和源代码都可以在Apache官网下载. 那么,就来开始编写第一个Str ...
随机推荐
- 环境变量之执行文件路径的变量PATH
当我们执行一个命令时,系统会依据PATH的设置去PATH定义的每个目录下查寻该命令的可执行文件,如果在PATH定义的目录中含有多个文件名为我们要执行的命令的可执行文件时,那么先查询到的同名命令先被执行 ...
- Android相机基础基于camera2API
前言 最近,在使用Android做一个照相机的开发.因为不能使用系统提供的相机应用,所以只能自己写一个.Android以前提供相机的api叫camera,不过在level 21被Google抛弃了.网 ...
- jQuery中的text(),html(),val()用法
jQuery中的text(),html(),val()用法 text():获取或者改变指定元素的文本 html():获取或改变指定元素的html元素以及文本 val():获取或者改变指定元素的valu ...
- eclipse debug 错误 之 processWorkerExit
eclipe 在debug模式下,有时候老是跳转到 ThreadPoolExecutor 之 processWorkerExit方法,很是让人恼火,是 因为在 java.util.concurrent ...
- ubuntu系统——增加磁盘空间
1.df查看磁盘使用情况 2.将windows下的磁盘空间分出与部分给ubuntu 3.格式化磁盘 在终端输入:mkfs -t ext3 /dev/sdb1 用ext3格式对/dev/sd ...
- OFBiz:解析doRequest()
这里的doRequest()是指RequestHandler中的同名函数: public void doRequest(HttpServletRequest request, HttpServletR ...
- 复制web项目,启动的时候的工程名如何改变
右键项目->properties 然后 搜索 web 出现 Web Project Settings 右边看见 Context root 改成你希望的名字. 然后重启elipse
- Linux命令-目录处理命令:cd
cd /tmp/shijiazhuang 切换到tmp目录下面的shijiazhuang目录 cd .. 切换到上一级目录
- 非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
非变动性算法代码分析与示例: 一.for_each C++ Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // TEMPLATE FUNCTION for_eac ...
- Apache重启报警,不存在虚拟主机目录(httpd.conf打开了一些扩展)
Apache重启时报警: AH00112: Warning: DocumentRoot [/usr/local/apache/docs/dummy-host.example.com] does not ...