ActionContextCleanUp的作用

延长act<wbr style="line-height:25px">ion中属性的生命周期,包括自定义属性,以便在jsp页面中进行访问,让actionContextcleanup过滤器来清除属性,不让act<wbr style="line-height:25px">ion自己清除。</wbr></wbr>

为了使用WebWork,我们只需要在web.xml配置FilterDispatcher一个过滤器即可,阅读一下FilterDispatcher的JavaDoc和源码,我们可以看到它调用了:

<wbr style="line-height:25px">finally<br style="line-height:25px"><wbr style="line-height:25px">{<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">ActionContextCleanUp.cleanUp(req);<br style="line-height:25px"><wbr style="line-height:25px">}<wbr style="line-height:25px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

在ActionContextCleanUp中,有这样的代码:

req.setAttribute(CLEANUP_PRESENT, Boolean.TRUE);<wbr style="line-height:25px"></wbr>

如果FilterDispatcher检测到这个属性,就不会清除ActionContext中的内容了,而由ActionContextCleanUp后续的代码来清除,保证了一系列的Filter访问正确的ActionContext.

文档中提到,如果用到SiteMesh的Filter或者其他类似Filter,那么设置顺序是:

<wbr style="line-height:25px">ActionContextCleanUp filter<br style="line-height:25px"><wbr style="line-height:25px">SiteMesh filter<br style="line-height:25px"><wbr style="line-height:25px">FilterDispatcher<br style="line-height:25px"><wbr style="line-height:25px">所以最后我们的web.xml应该类似这样:</wbr></wbr></wbr></wbr>

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-name&gt;ActionContextCleanUp&lt;/filter-name&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-class&gt;com.opensymphony.webwork.dispatcher.ActionContextCleanUp&lt;/filter-class&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;/filter&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-name&gt;sitemesh&lt;/filter-name&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-class&gt;com.opensymphony.webwork.sitemesh.FreeMarkerPageFilter&lt;/filter-class&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;/filter&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-name&gt;webwork&lt;/filter-name&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-class&gt;com.opensymphony.webwork.dispatcher.FilterDispatcher&lt;/filter-class&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;/filter&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-mapping&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-name&gt;ActionContextCleanUp&lt;/filter-name&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;url-pattern&gt;/*&lt;/url-pattern&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;/filter-mapping&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-mapping&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-name&gt;sitemesh&lt;/filter-name&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;url-pattern&gt;/*&lt;/url-pattern&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;/filter-mapping&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-mapping&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;filter-name&gt;webwork&lt;/filter-name&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;url-pattern&gt;/*&lt;/url-pattern&gt;<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">&lt;/filter-mapping&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


---------------------------------------------------------------------------------------------------------------------

在Struts 2.1.6之前,ActionContextCleanUp的完整路径是com.opensymphony.webwork.dispatcher.ActionContextCleanUp,现在的路径变成了org.apache.struts2.dispatcher.ActionContextCleanup

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">那么这个类究竟有什么用处呢?是不是一定要用呢?</wbr></wbr></wbr></wbr>

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">下面是这个类内部的注释。</wbr></wbr></wbr></wbr>

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">Special filter designed to work with the FilterDispatcher and allow<br style="line-height:25px"> for easier integration with SiteMesh. Normally, ordering your filters to have<br style="line-height:25px"> SiteMesh go first, and then FilterDispatcher go second is perfectly fine.<br style="line-height:25px"> However, sometimes you may wish to access Struts features, including the<br style="line-height:25px"> value stack, from within your SiteMesh decorators. Because FilterDispatcher<br style="line-height:25px"> cleans up the ActionContext, your decorator won't have access to the<br style="line-height:25px"> data you want.<br style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">By adding this filter, the FilterDispatcher will know to not clean up and<br style="line-height:25px"> instead defer cleanup to this filter. The ordering of the filters should then be:<br style="line-height:25px"> 1.this filter</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

2.SiteMesh filter

3.FilterDispatcher

<wbr style="line-height:25px"></wbr>

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">就是说,一般情况下,如果你要用SiteMesh或者其他过滤器,一般是放在FilterDispatcher或者是现在的StrutsPrepareAndExecuteF<wbr style="line-height:25px">ilter之前。在调用完所有过滤器的doFilter方法后,核心过滤器FilterDispatcher或者StrutsPrepareAndExecuteF<wbr style="line-height:25px">ilter会清空ActionContext,如果其他过滤器要一直使用value stack等struts的特性时,如果不用ActionContextCleanUp的话,便得不到想要的值。</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:25px"></wbr>

<wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px"><wbr style="line-height:25px">ActionContextCleanUp的作用就是上面用粗体标注出来的那一句。它会在doFilter方法里设置一个计数器counter的初始值为1,有了这个值,后续的核心过滤器就不会清空ActionContext,而是由之前的过滤器也就是ActionContextCleanUp来清空ActionContext。</wbr></wbr></wbr></wbr>





<wbr style="line-height:25px">------------------------------------------------------------------------------------------------------------------------------------------------------ <div style="line-height:25px"><br style="line-height:25px"></div> <div style="line-height:25px"> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> <strong>1. ActionContext</strong></p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> <br style="line-height:25px"> ActionContext是被存放在当前线程中的,获取ActionContext也是从ThreadLocal中获取的。所以在执行拦截器、 action和result的过程中,由于他们都是在一个线程中按照顺序执行的,所以可以可以在任意时候在ThreadLocal中获取 ActionContext。</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> <br style="line-height:25px"> ActionContext包括了很多信息,比如Session、Application、Request、Locale、ValueStack等,其中 ValueStack可以解析ognl表达式,来动态获取一些值,同时可以给表达式提供对象。</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> </p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器 (其实我们这里的容器就是一个Map而已),它存放的是Action在执行时需要用到的对象. 一般情况, 我们的ActionContext都是通过: ActionContext context = (ActionContext) actionContext.get(); 来获取的.我们再来看看这里的actionContext对象的创建:</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> static ThreadLocal actionContext = new ActionContextThreadLocal();</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> ActionContextThreadLocal是实现ThreadLocal的一个内部类.ThreadLocal可以命名为"线程局部变量",它为每一个使用该变量的线程都提供一个变量值的副本,使每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突.这样,我们 ActionContext里的属性只会在对应的当前请求线程中可见,从而保证它是线程安全的.</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> 通过ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession(); (通过Map模拟HttpServlet的对象,操作更方便)</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> </p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> <strong>2. ServletActionContext</strong></p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> ServletActionContext(com.opensymphony.webwork. ServletActionContext),这个类直接继承了我们上面介绍的ActionContext,它提供了直接与Servlet相关对象访问的功能,它可以取得的对象有:</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> (1)javax.servlet.http.HttpServletRequest : HTTPservlet请求对象<br style="line-height:25px"> (2)javax.servlet.http.HttpServletResponse : HTTPservlet相应对象<br style="line-height:25px"> (3)javax.servlet.ServletContext : Servlet上下文信息<br style="line-height:25px"> (4)javax.servlet.ServletConfig : Servlet配置对象<br style="line-height:25px"> (5)javax.servlet.jsp.PageContext : Http页面上下文</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> 如何从ServletActionContext里取得Servlet的相关对象:</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> &lt;1&gt;取得HttpServletRequest对象: HttpServletRequest request = ServletActionContext. getRequest();</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> &lt;2&gt;取得HttpSession对象: HttpSession session = ServletActionContext. getRequest().getSession();</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> </p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> <strong>3. ServletActionContext和ActionContext联系</strong></p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> ServletActionContext和ActionContext有着一些重复的功能,在我们的Action中,该如何去抉择呢?我们遵循的原则是:如果ActionContext能够实现我们的功能,那最好就不要使用ServletActionContext,让我们的Action尽量不要直接去访问Servlet的相关对象.</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> 注意:在使用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。</p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> </p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> <strong>4.ActionContextClearUp</strong></p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> ActionContextClearUp其实是Defer ClearUP.作用就是延长action中属性的生命周期,包括自定义属性,以便在jsp页面中进行访问,让actionContextcleanup过滤器来清除属性,不让action自己清除。具体看下面的代码,代码很简单:</p> <div style="line-height:25px; font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace; font-size:12px; width:679px; overflow:auto; margin-left:9px; padding:1px; word-wrap:break-word"> <div style="line-height:21px"> <div style="line-height:21px; padding:3px; margin:0px; font-weight:bold">Java代码<a title="复制代码" rel="nofollow" href="http://qidaoxp.iteye.com/blog/492387" style="color:rgb(16,138,198); line-height:21px; text-decoration:none"><img src="http://qidaoxp.iteye.com/images/icon_copy.gif" alt="复制代码" style="line-height:21px; border-width:0px; max-width:100%"></a> </div> </div> <ol style="line-height:1.4em; margin:0px 0px 1px; padding:2px 0px; font-size:1em; border:1px solid rgb(209,215,220); color:rgb(43,145,175)"> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"><strong><strong>publicvoid</strong></strong>doFilter(...){ </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">... </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span><strong><strong>try</strong></strong><span style="color:black; line-height:21px">{ </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">... </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span><span style="color:#082000; line-height:21px">//继续执行所配置的chain中的Filter</span><span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">chain.doFilter(request,response); </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">}</span><strong><strong>finally</strong></strong><span style="color:black; line-height:21px">{ </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span><span style="color:#082000; line-height:21px">//保证在所有动作执行完之后,调用cleanUp</span><span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">... </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">cleanUp(request); </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">} </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">} </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <strong><strong>protected</strong></strong><span style="color:black; line-height:21px"></span><strong><strong>static</strong></strong><span style="color:black; line-height:21px"></span><strong><strong>void</strong></strong><span style="color:black; line-height:21px">cleanUp(ServletRequestreq){ </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">... </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">ActionContext.setContext(</span><strong><strong>null</strong></strong><span style="color:black; line-height:21px">);</span><span style="color:#082000; line-height:21px">//清除ActionContext实例</span><span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">Dispatcher.setInstance(</span><strong><strong>null</strong></strong><span style="color:black; line-height:21px">);</span><span style="color:#082000; line-height:21px">//清除Dispatcher实例(Dispatcher主要是完成将url解析成对应的Action)</span><span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">}</span> </li> </ol> </div> <div id="" style="line-height:25px; font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace; font-size:12px; width:679px; overflow:auto; margin-left:9px; padding:1px; word-wrap:break-word"> <div style="line-height:21px"> <div style="line-height:21px; padding:3px; margin:0px; font-weight:bold">Java代码<a title="收藏这段代码" rel="nofollow" href="http://mujun1209.iteye.com/blog/794658" style="color:rgb(16,138,198); line-height:21px; text-decoration:none"><img src="http://mujun1209.iteye.com/images/icon_star.png" alt="收藏代码" style="line-height:21px; border-width:0px; max-width:100%"></a> </div> </div> <ol start="1" style="line-height:1.4em; margin:0px 0px 1px; padding:2px 0px; font-size:1em; border:1px solid rgb(209,215,220); color:rgb(43,145,175)"> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"><strong>publicvoid</strong>doFilter(...){</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">...</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"><strong>try</strong>{</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">...</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"><span style="color:#082000; line-height:21px">//继续执行所配置的chain中的Filter</span></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">chain.doFilter(request,response);</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">}<strong>finally</strong>{</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"><span style="color:#082000; line-height:21px">//保证在所有动作执行完之后,调用cleanUp</span></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">...</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">cleanUp(request);</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">}</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">}</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"><strong>protectedstaticvoid</strong>cleanUp(ServletRequestreq){</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">...</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">ActionContext.setContext(<strong>null</strong>);<span style="color:#082000; line-height:21px">//清除ActionContext实例</span></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">Dispatcher.setInstance(<strong>null</strong>);<span style="color:#082000; line-height:21px">//清除Dispatcher实例(Dispatcher主要是完成将url解析成对应的Action)</span></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">}</span> </li> </ol> </div> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> </p> <p style="line-height:25px; margin:0px; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"> 另外注明一下UtilTimerStack的push和pop是用来计算调用方法所执行的开始和结束时间,用来做性能测试的。用法如下:</p> <div style="line-height:25px; font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace; font-size:12px; width:679px; overflow:auto; margin-left:9px; padding:1px; word-wrap:break-word"> <div style="line-height:21px"> <div style="line-height:21px; padding:3px; margin:0px; font-weight:bold">Java代码<a title="复制代码" rel="nofollow" href="http://qidaoxp.iteye.com/blog/492387" style="color:rgb(16,138,198); line-height:21px; text-decoration:none"><img src="http://qidaoxp.iteye.com/images/icon_copy.gif" alt="复制代码" style="line-height:21px; border-width:0px; max-width:100%"></a> </div> </div> <ol style="line-height:1.4em; margin:0px 0px 1px; padding:2px 0px; font-size:1em; border:1px solid rgb(209,215,220); color:rgb(43,145,175)"> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">StringtimerKey=<span style="color:blue; line-height:21px">"ActionContextCleanUp_doFilter:"</span>; </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">UtilTimerStack.setActive(</span><strong><strong>true</strong></strong><span style="color:black; line-height:21px">); </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">UtilTimerStack.push(timerKey); </span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:#082000; line-height:21px">//调用要测试的方法。</span><span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">UtilTimerStack.pop(timerKey);</span> </li> </ol> </div> <div id="" style="line-height:25px; font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace; font-size:12px; width:679px; overflow:auto; margin-left:9px; padding:1px; word-wrap:break-word"> <div style="line-height:21px"> <div style="line-height:21px; padding:3px; margin:0px; font-weight:bold">Java代码<a title="收藏这段代码" rel="nofollow" href="http://mujun1209.iteye.com/blog/794658" style="color:rgb(16,138,198); line-height:21px; text-decoration:none"><img src="http://mujun1209.iteye.com/images/icon_star.png" alt="收藏代码" style="line-height:21px; border-width:0px; max-width:100%"></a> </div> </div> <ol start="1" style="line-height:1.4em; margin:0px 0px 1px; padding:2px 0px; font-size:1em; border:1px solid rgb(209,215,220); color:rgb(43,145,175)"> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">StringtimerKey=<span style="color:blue; line-height:21px">"ActionContextCleanUp_doFilter:"</span>;</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">UtilTimerStack.setActive(<strong>true</strong>);</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">UtilTimerStack.push(timerKey);</span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px"><span style="color:#082000; line-height:21px">//调用要测试的方法。</span></span> </li> <li style="line-height:18px; font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left:1px solid rgb(209,215,220); background-color:rgb(250,250,250)"> <span style="color:black; line-height:21px">UtilTimerStack.pop(timerKey); </span> </li> </ol> </div> </div> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值