??xml version="1.0" encoding="utf-8" standalone="yes"?>国产中文一区二区三区,欧美精品久久久久久久久老牛影院,91欧美在线视频http://www.aygfsteel.com/hellxoul/category/50246.htmlzh-cnFri, 14 Dec 2012 07:45:47 GMTFri, 14 Dec 2012 07:45:47 GMT60Struts2中的拦截?/title><link>http://www.aygfsteel.com/hellxoul/archive/2012/12/14/392970.html</link><dc:creator>hellxoul</dc:creator><author>hellxoul</author><pubDate>Fri, 14 Dec 2012 02:41:00 GMT</pubDate><guid>http://www.aygfsteel.com/hellxoul/archive/2012/12/14/392970.html</guid><wfw:comment>http://www.aygfsteel.com/hellxoul/comments/392970.html</wfw:comment><comments>http://www.aygfsteel.com/hellxoul/archive/2012/12/14/392970.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/hellxoul/comments/commentRss/392970.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/hellxoul/services/trackbacks/392970.html</trackback:ping><description><![CDATA[<div>接下来,我们重点讨Z下Struts2中的拦截器的内部l构和执行顺序,q结合源码进行分析? <div class="wmqeeuq" id="wiki_menu"> <h4>??<a title="隐藏/昄目录"><small>[ - ]</small></a></h4> <ol><li><a >Interceptorl构</a></li><li><a >Interceptor执行分析</a></li><li><a >源码解析</a></li></ol> </div> <h2> Interceptorl构 <a name="1678" ><img alt="Top" src="http://www.iteye.com/images/wiki/top.gif?1324994303" /></a> </h2> <div> 让我们再来回一下之前我们曾l用q的一张Action LifeCycle的图Q?<br /> <br /><img src="http://www.iteye.com/upload/attachment/68182/ae963ed3-fae7-3710-bfcf-2fc49942ee90.png" alt="" /> <br /> <br />图中Q我们可以发玎ͼStruts2的Interceptor一层一层,把Action包裹在最里面。这Ll构Q大概有以下一些特点: <br /> <br /><strong>1. 整个l构如同一个堆栈,除了Action以外Q堆栈中的其他元素是Interceptor</strong> <br /> <br /><strong>2. Action位于堆栈的底部。由于堆?先进后出"的特性,如果我们试图把Action拿出来执行,我们必须首先把位于Action上端的Interceptor拿出来执行。这P整个执行Ş成了一个递归调用</strong> <br /> <br /><strong>3. 每个位于堆栈中的InterceptorQ除了需要完成它自n的逻辑Q还需要完成一个特D的执行职责。这个执行职责有3U选择Q?<br /> <br /><span style="color: blue;">1) 中止整个执行Q直接返回一个字W串作ؓresultCode</span> <br /> <br /><span style="color: blue;">2) 通过递归调用负责调用堆栈中下一个Interceptor的执?/span> <br /> <br /><span style="color: blue;">3) 如果在堆栈内已经不存在Q何的InterceptorQ调用Action</span> <br /></strong> <br /> <br />Struts2的拦截器l构的设计,实际上是一个典型的<strong>责Q链模?/strong>的应用。首先将整个执行划分成若q相同类型的元素Q每个元素具备不同的逻辑责QQƈ他们纳入到一个链式的数据l构中(我们可以把堆栈结构也看作是一个递归的链式结构)Q而每个元素又有责任负责链式结构中下一个元素的执行调用?<br /> <br />q样的设计,从代码重构的角度来看Q实际上是将一个复杂的pȝQ分而治之,从而得每个部分的逻辑能够高度重用q具备高度可扩展性。所以,Interceptorl构实在是Struts2/Xwork设计中的_֍之笔? </div> <h2> Interceptor执行分析 <a name="1679" ><img alt="Top" src="http://www.iteye.com/images/wiki/top.gif?1324994303" /></a> </h2> <div> <strong><span style="color: blue;">Interceptor的定?/span></strong> <br /> <br />我们来看一下Interceptor的接口的定义Q?<br /> <br /><div class="wmqeeuq" id=""><div><div>Java代码  <a title="收藏q段代码"><img src="http://struts2.group.iteye.com/images/icon_star.png" alt="收藏代码" /></a></div></div><ol start="1"><li><span>public interface Interceptor extends Serializable {  </span></li><li>  </li><li>    <span>/** </span></li><li><span>     * Called to let an interceptor clean up any resources it has allocated. </span></li><li><span>     */  </span></li><li>    <span>void destroy();  </span></li><li>  </li><li>    <span>/** </span></li><li><span>     * Called after an interceptor is created, but before any requests are processed using </span></li><li><span>     * {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving </span></li><li><span>     * the Interceptor a chance to initialize any needed resources. </span></li><li><span>     */  </span></li><li>    <span>void init();  </span></li><li>  </li><li>    <span>/** </span></li><li><span>     * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the </span></li><li><span>     * request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code. </span></li><li><span>     * </span></li><li><span>     * @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself. </span></li><li><span>     * @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}. </span></li><li><span>     */  </span></li><li>    String intercept(ActionInvocation invocation) <span>throws Exception;  </span></li><li>}  </li></ol></div> <br /> <br />Interceptor的接口定义没有什么特别的地方Q除了init和destoryҎ以外QinterceptҎ是实现整个拦截器机制的核心方法。而它所依赖的参数ActionInvocation则是我们之前章节中曾l提到过的著名的<strong>Action调度?/strong>?<br /> <br />我们再来看看一个典型的Interceptor的抽象实现类Q?<br /> <br /><div class="wmqeeuq" id=""><div><div>Java代码  <a title="收藏q段代码"><img src="http://struts2.group.iteye.com/images/icon_star.png" alt="收藏代码" /></a></div></div><ol start="1"><li><span>public abstract class AroundInterceptor extends AbstractInterceptor {  </span></li><li>      </li><li>    <span>/* (non-Javadoc) </span></li><li><span>     * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation) </span></li><li><span>     */  </span></li><li>    <span>@Override  </span></li><li>    <span>public String intercept(ActionInvocation invocation) throws Exception {  </span></li><li>        String result = <span>null;  </span></li><li>  </li><li>        before(invocation);  </li><li>        <span>// 调用下一个拦截器Q如果拦截器不存在,则执行Action  </span></li><li>        result = invocation.invoke();  </li><li>        after(invocation, result);  </li><li>  </li><li>        <span>return result;  </span></li><li>    }  </li><li>      </li><li>    <span>public abstract void before(ActionInvocation invocation) throws Exception;  </span></li><li>  </li><li>    <span>public abstract void after(ActionInvocation invocation, String resultCode) throws Exception;  </span></li><li>  </li><li>}  </li></ol></div> <br /> <br />在这个实现类中,实际上已l实C最单的拦截器的雏Ş。或许大家对q样的代码还比较陌生Q这没有关系。我在这里需要指出的是一个很重要的方?invocation.invoke()。这是ActionInvocation中的ҎQ而ActionInvocation是Action调度者,所 以这个方法具备以?层含义: <br /> <br /><strong>1. 如果拦截器堆栈中q有其他的InterceptorQ那么invocation.invoke()调用堆栈中下一个Interceptor的执行?/strong> <br /> <br /><strong>2. 如果拦截器堆栈中只有Action了,那么invocation.invoke()调用Action执行?/strong> <br /> <br />所以,我们可以发现Qinvocation.invoke()q个Ҏ其实是整个拦截器框架的实现核心。基于这L实现机制Q我们还可以得到下面2个非帔R要的推论Q?<br /> <br /><strong>1. 如果在拦截器中,我们不用invocation.invoke()来完成堆栈中下一个元素的调用Q而是直接q回一个字W串作ؓ执行l果Q那么整个执行将被中止?/strong> <br /> <br /><strong>2. 我们可以以invocation.invoke()为界Q将拦截器中的代码分?个部分,在invocation.invoke()之前的代码,会?Action之前被依ơ执行,而在invocation.invoke()之后的代码,会在Action之后被逆序执行?/strong> <br /> <br />由此Q我们就可以通过invocation.invoke()作ؓAction代码真正的拦截点Q从而实现AOP?<br /> <br /><strong><span style="color: blue;">Interceptor拦截cd</span></strong> <br /> <br />从上面的分析Q我们知道,整个拦截器的核心部分是invocation.invoke()q个函数的调用位|。事实上Q我们也正式Ҏq句代码的调用位|,来进行拦截类型的区分的。在Struts2中,Interceptor的拦截类型,分成以下三类Q?<br /> <br /><strong>1. before</strong> <br /> <br />before拦截Q是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之前。这些代码,依照拦截器定义的顺序,<strong>序执行</strong>?<br /> <br /><strong>2. after</strong> <br /> <br />after拦截Q是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之后。这些代码,一招拦截器定义的顺序,<strong>逆序执行</strong>?<br /> <br />3. PreResultListener <br /> <br />有的时候,before拦截和after拦截Ҏ们来说是不够的,因ؓ我们需要在Action执行完之后,但是q没有回到视囑ֱ之前Q做一些事 情。Struts2同样支持q样的拦截,q种拦截方式Q是通过在拦截器中注册一个PreResultListener的接口来实现的?<br /> <br /><div class="wmqeeuq" id=""><div><div>Java代码  <a title="收藏q段代码"><img src="http://struts2.group.iteye.com/images/icon_star.png" alt="收藏代码" /></a></div></div><ol start="1"><li><span>public interface PreResultListener {  </span></li><li>  </li><li>    <span>/** </span></li><li><span>     * This callback method will be called after the Action execution and before the Result execution. </span></li><li><span>     * </span></li><li><span>     * @param invocation </span></li><li><span>     * @param resultCode </span></li><li><span>     */  </span></li><li>    <span>void beforeResult(ActionInvocation invocation, String resultCode);  </span></li><li>}  </li></ol></div> <br /> <br />在这里,我们看到QStruts2能够支持如此多的拦截cdQ与其本w的数据l构和整体设计有很大的关pR正如我在之前的文章中所提到的: <br /> <br /><div>downpour 写道</div><div>因ؓAction是一个普通的Javac,而不是一个Servletc,完全q于Web容器Q所以我们就能够更加方便地对Control层进行合理的层次设计Q从而抽象出许多公共的逻辑Qƈ这些逻辑q出Action对象本n?/div> <br /> <br />我们可以看到QStruts2对于整个执行的划分,从Interceptor到Action一直到ResultQ每一层都职责明确。不仅如此,Struts2qؓ每一个层ơ之前都讄了恰如其分的插入炏V得整个Action层的扩展性得C史无前例的提升?<br /> <br /><strong><span style="color: blue;">Interceptor执行序</span></strong> <br /> <br />Interceptor的执行顺序或许是我们在整个过E中最最兛_的部分。根据上面所提到的概念,我们实际上已l能够大致明白了Interceptor的执行机理。我们来看看Struts2的Reference对Interceptor执行序的一个Ş象的例子?<br /> <br />如果我们有一个interceptor-stack的定义如下: <br /> <br /><div class="wmqeeuq" id=""><div><div>Xml代码  <a title="收藏q段代码"><img src="http://struts2.group.iteye.com/images/icon_star.png" alt="收藏代码" /></a></div></div><ol start="1"><li><span><interceptor-stack name="xaStack">  </span></li><li>  <span><interceptor-ref name="thisWillRunFirstInterceptor"/>  </span></li><li>  <span><interceptor-ref name="thisWillRunNextInterceptor"/>  </span></li><li>  <span><interceptor-ref name="followedByThisInterceptor"/>  </span></li><li>  <span><interceptor-ref name="thisWillRunLastInterceptor"/>  </span></li><li><span></interceptor-stack>  </span></li></ol></div> <br /> <br />那么Q整个执行的序大概像这P <br /> <br /><img src="http://struts2.group.iteye.com/upload/attachment/71392/23045c94-b72a-3c04-9c6c-06ad4392d743.gif" alt="" /> <br /> <br />在这里,我稍微改了一下Struts2的Reference中的执行序CZQ得整个执行顺序更加能够被理解。我们可以看刎ͼ递归调用保证了各U各L拦截cd的执行能够井井有条?<br /> <br />h意在q里Q每个拦截器中的代码的执行顺序,在Action之前Q拦截器的执行顺序与堆栈中定义的一_而在Action和Result之后Q拦截器的执行顺序与堆栈中定义的序相反?<br /> </div> <h2> 源码解析 <a name="1680" ><img alt="Top" src="http://www.iteye.com/images/wiki/top.gif?1324994303" /></a> </h2> 接下来我们就来看看源码,看看Struts2是如何保证拦截器、Action与Result三者之间的执行序的?<br /> <br />之前我曾l提刎ͼActionInvocation是Struts2中的调度器,所以事实上Q这些代码的调度执行Q是?ActionInvocation的实现类中完成的Q这里,我抽取了DefaultActionInvocation中的invoke()ҎQ它向?们展CZ切?<br /> <br /><div class="wmqeeuq" id=""><div><div>Java代码  <a title="收藏q段代码"><img src="http://struts2.group.iteye.com/images/icon_star.png" alt="收藏代码" /></a></div></div><ol start="1"><li><span>/** </span></li><li><span> * @throws ConfigurationException If no result can be found with the returned code </span></li><li><span> */  </span></li><li><span>public String invoke() throws Exception {  </span></li><li>    String profileKey = <span>"invoke: ";  </span></li><li>    <span>try {  </span></li><li>        UtilTimerStack.push(profileKey);  </li><li>              </li><li>        <span>if (executed) {  </span></li><li>            <span>throw new IllegalStateException("Action has already executed");  </span></li><li>        }  </li><li>        <span>// 依次调用拦截器堆栈中的拦截器代码执行  </span></li><li>        <span>if (interceptors.hasNext()) {  </span></li><li>            <span>final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();  </span></li><li>            UtilTimerStack.profile(<span>"interceptor: "+interceptor.getName(),   </span></li><li>                    <span>new UtilTimerStack.ProfilingBlock<String>() {  </span></li><li>                        <span>public String doProfiling() throws Exception {  </span></li><li>                         <span>// ActionInvocation作ؓ参数Q调用interceptor中的interceptҎ执行  </span></li><li>                            resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.<span>this);  </span></li><li>                            <span>return null;  </span></li><li>                        }  </li><li>            });  </li><li>        } <span>else {  </span></li><li>            resultCode = invokeActionOnly();  </li><li>        }  </li><li>  </li><li>        <span>// this is needed because the result will be executed, then control will return to the Interceptor, which will  </span></li><li>        <span>// return above and flow through again  </span></li><li>        <span>if (!executed) {  </span></li><li>            <span>// 执行PreResultListener  </span></li><li>            <span>if (preResultListeners != null) {  </span></li><li>                <span>for (Iterator iterator = preResultListeners.iterator();  </span></li><li>                    iterator.hasNext();) {  </li><li>                    PreResultListener listener = (PreResultListener) iterator.next();  </li><li>                          </li><li>                    String _profileKey=<span>"preResultListener: ";  </span></li><li>                    <span>try {  </span></li><li>                            UtilTimerStack.push(_profileKey);  </li><li>                            listener.beforeResult(<span>this, resultCode);  </span></li><li>                    }  </li><li>                    <span>finally {  </span></li><li>                            UtilTimerStack.pop(_profileKey);  </li><li>                    }  </li><li>                }  </li><li>            }  </li><li>  </li><li>            <span>// now execute the result, if we're supposed to  </span></li><li>            <span>// action与interceptor执行完毕Q执行Result  </span></li><li>            <span>if (proxy.getExecuteResult()) {  </span></li><li>                executeResult();  </li><li>            }  </li><li>  </li><li>            executed = <span>true;  </span></li><li>        }  </li><li>  </li><li>        <span>return resultCode;  </span></li><li>    }  </li><li>    <span>finally {  </span></li><li>        UtilTimerStack.pop(profileKey);  </li><li>    }  </li><li>}  </li></ol></div> <br /> <br />从源码中Q我们可以看刎ͼ我们之前提到的Struts2的Action层的4个不同的层次Q在q个Ҏ中都有体玎ͼ他们分别是:拦截?QInterceptorQ、Action、PreResultListener和Result。在q个Ҏ中,保证了这些层ơ的有序调用和执行。由此我 们也可以看出<strong><span style="color: red;">Struts2在Action层次设计上的众多考虑Q每个层ơ都具备了高度的扩展性和插入点,使得E序员可以在M喜欢的层ơ加入自q实现机制改变Action的行为?/span></strong> <br /> <br />在这里,需要特别强调的Q是其中拦截器部分的执行调用Q?<br /> <br /><div class="wmqeeuq" id=""><div><div>Java代码  <a title="收藏q段代码"><img src="http://struts2.group.iteye.com/images/icon_star.png" alt="收藏代码" /></a></div></div><ol start="1"><li><span>resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);  </span></li></ol></div> <br /> <br />表面上,它只是执行了拦截器中的interceptҎQ如果我们结合拦截器来看Q就能看出点端倪来Q?<br /> <br /><div class="wmqeeuq" id=""><div><div>Java代码  <a title="收藏q段代码"><img src="http://struts2.group.iteye.com/images/icon_star.png" alt="收藏代码" /></a></div></div><ol start="1"><li><span>public String intercept(ActionInvocation invocation) throws Exception {  </span></li><li>    String result = <span>null;  </span></li><li>  </li><li>        before(invocation);  </li><li>        <span>// 调用invocation的invoke()ҎQ在q里形成了递归调用  </span></li><li>        result = invocation.invoke();  </li><li>        after(invocation, result);  </li><li>  </li><li>        <span>return result;  </span></li><li>}  </li></ol></div> <br /> <br />原来在intercept()Ҏ又对ActionInvocation的invoke()Ҏq行递归调用QActionInvocation 循环嵌套在intercept()中,一直到语句result = invocation.invoke()执行l束。这PInterceptor又会按照刚开始执行的逆向序依次执行l束?/div><img src ="http://www.aygfsteel.com/hellxoul/aggbug/392970.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/hellxoul/" target="_blank">hellxoul</a> 2012-12-14 10:41 <a href="http://www.aygfsteel.com/hellxoul/archive/2012/12/14/392970.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ָ</a>| <a href="http://" target="_blank">̩</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">̨</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank">ɰ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">կ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ԫ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ζ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ٽ</a>| <a href="http://" target="_blank">ƽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ں</a>| <a href="http://" target="_blank">IJ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ͬ</a>| <a href="http://" target="_blank">ͤ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">®</a>| <a href="http://" target="_blank"></a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>