当前位置: 首页 > news >正文

网站建设方案范本某网站项目策划书

网站建设方案范本,某网站项目策划书,公司网站建设方案书,桂林市网站建设在之前的文章中#xff0c;我们已经涉及到了拦截器#xff08;Interceptor#xff09;的概念。 downpour 写道拦截器是AOP中的概念#xff0c;它本身是一段代码#xff0c;可以通过定义“织入点”#xff0c;来指定拦截器的代码在“织入点”的前后执行#xff0c;从而起… 在之前的文章中我们已经涉及到了拦截器Interceptor的概念。  downpour 写道 拦截器是AOP中的概念它本身是一段代码可以通过定义“织入点”来指定拦截器的代码在“织入点”的前后执行从而起到拦截的作用。正如上面 Struts2的Reference中讲述的Struts2的Interceptor其拦截的对象是Action代码可以定义在Action代码之前或者之后执行拦截器的代码。 接下来我们将重点讨论一下Struts2中的拦截器的内部结构和执行顺序并结合源码进行分析。 Interceptor结构 我们可以发现Struts2的Interceptor一层一层把Action包裹在最里面。这样的结构大概有以下一些特点 1. 整个结构就如同一个堆栈除了Action以外堆栈中的其他元素是Interceptor 2. Action位于堆栈的底部。由于堆栈先进后出的特性如果我们试图把Action拿出来执行我们必须首先把位于Action上端的Interceptor拿出来执行。这样整个执行就形成了一个递归调用 3. 每个位于堆栈中的Interceptor除了需要完成它自身的逻辑还需要完成一个特殊的执行职责。这个执行职责有3种选择 1) 中止整个执行直接返回一个字符串作为resultCode 2) 通过递归调用负责调用堆栈中下一个Interceptor的执行 3) 如果在堆栈内已经不存在任何的Interceptor调用Action Struts2的拦截器结构的设计实际上是一个典型的责任链模式的应用。首先将整个执行划分成若干相同类型的元素每个元素具备不同的逻辑责任并将他们纳入到一个链式的数据结构中我们可以把堆栈结构也看作是一个递归的链式结构而每个元素又有责任负责链式结构中下一个元素的执行调用。 这样的设计从代码重构的角度来看实际上是将一个复杂的系统分而治之从而使得每个部分的逻辑能够高度重用并具备高度可扩展性。所以Interceptor结构实在是Struts2/Xwork设计中的精华之笔。 Interceptor执行分析 Interceptor的定义 我们来看一下Interceptor的接口的定义  Java代码   public interface Interceptor extends Serializable {        /**      * Called to let an interceptor clean up any resources it has allocated.      */      void destroy();        /**      * Called after an interceptor is created, but before any requests are processed using      * {link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving      * the Interceptor a chance to initialize any needed resources.      */      void init();        /**      * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the      * request by the {link ActionInvocation} or to short-circuit the processing and just return a String return code.      *      * return the return code, either returned from {link ActionInvocation#invoke()}, or from the interceptor itself.      * throws Exception any system-level error, as defined in {link com.opensymphony.xwork2.Action#execute()}.      */      String intercept(ActionInvocation invocation) throws Exception;  }   Interceptor的接口定义没有什么特别的地方除了init和destory方法以外intercept方法是实现整个拦截器机制的核心方法。而它所依赖的参数ActionInvocation则是我们之前章节中曾经提到过的著名的Action调度者。 我们再来看看一个典型的Interceptor的抽象实现类  Java代码   public abstract class AroundInterceptor extends AbstractInterceptor {            /* (non-Javadoc)      * see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)      */      Override      public String intercept(ActionInvocation invocation) throws Exception {          String result  null;            before(invocation);          // 调用下一个拦截器如果拦截器不存在则执行Action          result  invocation.invoke();          after(invocation, result);            return result;      }            public abstract void before(ActionInvocation invocation) throws Exception;        public abstract void after(ActionInvocation invocation, String resultCode) throws Exception;    }   在这个实现类中实际上已经实现了最简单的拦截器的雏形。或许大家对这样的代码还比较陌生这没有关系。我在这里需要指出的是一个很重要的方法invocation.invoke()。这是ActionInvocation中的方法而ActionInvocation是Action调度者所以这个方法具备以下2层含义 1. 如果拦截器堆栈中还有其他的Interceptor那么invocation.invoke()将调用堆栈中下一个Interceptor的执行。 2. 如果拦截器堆栈中只有Action了那么invocation.invoke()将调用Action执行。 所以我们可以发现invocation.invoke()这个方法其实是整个拦截器框架的实现核心。基于这样的实现机制我们还可以得到下面2个非常重要的推论 1. 如果在拦截器中我们不使用invocation.invoke()来完成堆栈中下一个元素的调用而是直接返回一个字符串作为执行结果那么整个执行将被中止。 2. 我们可以以invocation.invoke()为界将拦截器中的代码分成2个部分在invocation.invoke()之前的代码将会在Action之前被依次执行而在invocation.invoke()之后的代码将会在Action之后被逆序执行。 由此我们就可以通过invocation.invoke()作为Action代码真正的拦截点从而实现AOP。 Interceptor拦截类型 从上面的分析我们知道整个拦截器的核心部分是invocation.invoke()这个函数的调用位置。事实上我们也正式根据这句代码的调用位置来进行拦截类型的区分的。在Struts2中Interceptor的拦截类型分成以下三类 1. before before拦截是指在拦截器中定义的代码它们存在于invocation.invoke()代码执行之前。这些代码将依照拦截器定义的顺序顺序执行。 2. after after拦截是指在拦截器中定义的代码它们存在于invocation.invoke()代码执行之后。这些代码将一招拦截器定义的顺序逆序执行。 3. PreResultListener 有的时候before拦截和after拦截对我们来说是不够的因为我们需要在Action执行完之后但是还没有回到视图层之前做一些事情。Struts2同样支持这样的拦截这种拦截方式是通过在拦截器中注册一个PreResultListener的接口来实现的。  Java代码   public interface PreResultListener {        /**      * This callback method will be called after the Action execution and before the Result execution.      *      * param invocation      * param resultCode      */      void beforeResult(ActionInvocation invocation, String resultCode);  }   在这里我们看到Struts2能够支持如此多的拦截类型与其本身的数据结构和整体设计有很大的关系。正如我在之前的文章中所提到的  downpour 写道 因为Action是一个普通的Java类而不是一个Servlet类完全脱离于Web容器所以我们就能够更加方便地对Control层进行合理的层次设计从而抽象出许多公共的逻辑并将这些逻辑脱离出Action对象本身。 我们可以看到Struts2对于整个执行的划分从Interceptor到Action一直到Result每一层都职责明确。不仅如此Struts2还为每一个层次之前都设立了恰如其分的插入点。使得整个Action层的扩展性得到了史无前例的提升。Interceptor执行顺序 Interceptor的执行顺序或许是我们在整个过程中最最关心的部分。根据上面所提到的概念我们实际上已经能够大致明白了Interceptor的执行机理。我们来看看Struts2的Reference对Interceptor执行顺序的一个形象的例子。 如果我们有一个interceptor-stack的定义如下  Xml代码   interceptor-stack namexaStack    interceptor-ref namethisWillRunFirstInterceptor/    interceptor-ref namethisWillRunNextInterceptor/    interceptor-ref namefollowedByThisInterceptor/    interceptor-ref namethisWillRunLastInterceptor/  /interceptor-stack   在这里我稍微改了一下Struts2的Reference中的执行顺序示例使得整个执行顺序更加能够被理解。我们可以看到递归调用保证了各种各样的拦截类型的执行能够井井有条。 请注意在这里每个拦截器中的代码的执行顺序在Action之前拦截器的执行顺序与堆栈中定义的一致而在Action和Result之后拦截器的执行顺序与堆栈中定义的顺序相反。 源码解析 接下来我们就来看看源码看看Struts2是如何保证拦截器、Action与Result三者之间的执行顺序的。 springmvc4 mybatis 整合 框架源码 bootstrap html5 MySQL oracle sqlsever spring SSM 下载地址  之前我曾经提到ActionInvocation是Struts2中的调度器所以事实上这些代码的调度执行是在ActionInvocation的实现类中完成的这里我抽取了DefaultActionInvocation中的invoke()方法它将向我们展示一切。  Java代码   /**  * throws ConfigurationException If no result can be found with the returned code  */  public String invoke() throws Exception {      String profileKey  invoke: ;      try {          UtilTimerStack.push(profileKey);                        if (executed) {              throw new IllegalStateException(Action has already executed);          }          // 依次调用拦截器堆栈中的拦截器代码执行          if (interceptors.hasNext()) {              final InterceptorMapping interceptor  (InterceptorMapping) interceptors.next();              UtilTimerStack.profile(interceptor: interceptor.getName(),                       new UtilTimerStack.ProfilingBlockString() {                          public String doProfiling() throws Exception {                           // 将ActionInvocation作为参数调用interceptor中的intercept方法执行                              resultCode  interceptor.getInterceptor().intercept(DefaultActionInvocation.this);                              return null;                          }              });          } else {              resultCode  invokeActionOnly();          }            // this is needed because the result will be executed, then control will return to the Interceptor, which will          // return above and flow through again          if (!executed) {              // 执行PreResultListener              if (preResultListeners ! null) {                  for (Iterator iterator  preResultListeners.iterator();                      iterator.hasNext();) {                      PreResultListener listener  (PreResultListener) iterator.next();                                                String _profileKeypreResultListener: ;                      try {                              UtilTimerStack.push(_profileKey);                              listener.beforeResult(this, resultCode);                      }                      finally {                              UtilTimerStack.pop(_profileKey);                      }                  }              }                // now execute the result, if were supposed to              // action与interceptor执行完毕执行Result              if (proxy.getExecuteResult()) {                  executeResult();              }                executed  true;          }            return resultCode;      }      finally {          UtilTimerStack.pop(profileKey);      }  }   从源码中我们可以看到我们之前提到的Struts2的Action层的4个不同的层次在这个方法中都有体现他们分别是拦截器Interceptor、Action、PreResultListener和Result。在这个方法中保证了这些层次的有序调用和执行。由此我们也可以看出Struts2在Action层次设计上的众多考虑每个层次都具备了高度的扩展性和插入点使得程序员可以在任何喜欢的层次加入自己的实现机制改变Action的行为。 在这里需要特别强调的是其中拦截器部分的执行调用  Java代码   resultCode  interceptor.getInterceptor().intercept(DefaultActionInvocation.this);   表面上它只是执行了拦截器中的intercept方法如果我们结合拦截器来看就能看出点端倪来  Java代码   public String intercept(ActionInvocation invocation) throws Exception {      String result  null;            before(invocation);          // 调用invocation的invoke()方法在这里形成了递归调用          result  invocation.invoke();          after(invocation, result);            return result;  }   原来在intercept()方法又对ActionInvocation的invoke()方法进行递归调用ActionInvocation循环嵌套在intercept()中一直到语句result invocation.invoke()执行结束。这样Interceptor又会按照刚开始执行的逆向顺序依次执行结束。 springmvc4 mybatis 整合 框架源码 bootstrap html5 mysql oracle sqlsever spring SSM 下载地址  一个有序链表通过递归调用变成了一个堆栈执行过程将一段有序执行的代码变成了2段执行顺序完全相反的代码过程从而巧妙地实现了AOP。这也就成为了Struts2的Action层的AOP基础。 转载于:https://www.cnblogs.com/guanfang508/p/5473359.html
http://www.pierceye.com/news/190838/

相关文章:

  • 克拉玛依 网站建设红圈工程项目管理软件
  • 北京网站ui设计公司共青城网站建设公司
  • 电子商务网站设计说明书开发一个网站
  • 网站制作长沙怎么做淘客手机网站
  • 五路居网站建设wordpress php允许上传文件大小
  • 旅游网站的设计代码下列哪些不属于企业网站建设基本原则
  • 房屋租赁网站开发意义做男鞋的网站
  • 网站负责人可以备案北京建设部网站 信息中心
  • 网站建设分录怎么开四川省城乡住房建设部网站首页
  • 刘家窑网站建设公司如何在网络上推广产品
  • 全球建站东莞市的网站公司哪家好
  • 地方网站发展怎么做链接推广产品
  • 上海制造网站公司网站优化做网站优化
  • vs2012做网站wordpress 页眉
  • 北网站建设重庆建设工程查询网站
  • 给我做网站的人老是给我留点尾巴太原本地网站搭建公司
  • 静态页面网站站标代码写进到静态页面了 怎么不显示呢?自助建站网
  • 免费在线代理网站微信会员卡管理系统
  • 和动物做的网站吗做网站销售怎么样
  • 宝塔面板做织梦网站深圳龙华大浪做网站公司
  • 阿里云建网站费用上海网站建设觉策动力
  • 电子商务网站的建设与维护方法h5第三方收款平台
  • 网站建设所需人力设计广告网站
  • php网站发送邮件动态倒计时网站模板
  • 温州建设网站制作wordpress调用文章简介
  • 大庆市建设局网站刘东科技公司做网站
  • 山东助企网站建设宁波百度关键词推广
  • 宜昌市夷陵区建设局网站培训推广 seo
  • 怎样做安居客网站用幽默的语言来形容网站开发
  • 宜昌网站建设选择宜昌慧享互动手机怎么防止网站跳转