自定义拦截器分析

来源:互联网 发布:淘宝网购买泓铭软件 编辑:程序博客网 时间:2024/06/06 14:16

    在Struts2 中很多功能都是由拦截器完成的比如 模型驱动,动态参数封装。 它是AOP 编程思想的一种应用形式。

    

     类似于servlet 中的过滤器  比如有  1  ,2   3,  3个过滤器他的执行顺序是  先根据注册的顺序执行 1 ,2 ,3  过滤器 然后进入action  再进入到jSP 最后再执行 3,2,1  完之后显示  页面。

    进入 ServletConfigInterceptor 类看到 继承于AbstractInterceptor    接下来进到

public abstract class AbstractInterceptor implements Interceptor {    /**     * Does nothing     */    public void init() {    }        /**     * Does nothing     */    public void destroy() {    }    /**     * Override to handle interception     */    public abstract String intercept(ActionInvocation invocation) throws Exception;}
看到代码是这样的  进入Interceptor 看到源码如下

   

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.     *     * @param invocation the action invocation     * @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;}
 看到3个方法 一个是初始化 一个的销毁  中间的执行过滤。 就可以猜到这个类大概是一个单列模式。


 既然struts2 的过滤器 是 继承于 AbstractInterceptor   我们就可以自定义一个过滤器

  可以直接继承于AbstractInterceptor    但是一般不会这么做  在开发中可以直接继承于MethodFilterInterceptor   因为过滤器的作用是过滤 用户发出的请求  这个类的作用就是 能手动控制 哪些请求可以过滤哪些 不能过滤。

  新建一个过滤器类 CheckLoginInterceptor   继承于MethodFilterInterceptor 实现  doIntercept

 接下来 修改配置文件定义一个全局结果视图   

      <global-results><!-- 全局结果视图 -->
     <result name="input">/login.jsp</result>
      </global-results>
      所有返回input 的请求都会跳转到login.jsp

     使用 interceptors标签创建一个过滤器组

    把我们自定义的过滤器 加进去

  

     <!-- 申明自定义拦截器 -->     <interceptors>                <interceptor name="checkLogin" class="com.heima.interceptor.CheckLoginInterceptor"></interceptor>     <interceptor-stack name="myDefault">  <!-- 创建过滤器组 -->           <interceptor-ref name="defaultStack"> <!-- STUTRS2 默认过滤器 -->           </interceptor-ref>           <interceptor-ref name="checkLogin"> <!-- 把自定义的过滤器加入到 群组中 -->           </interceptor-ref>     </interceptor-stack>     </interceptors>

 需要默认所有动作都使用过滤器

     <!-- 默认所有action 都会执行 拦截器 -->
      <default-interceptor-ref name="myDefault">
      
      </default-interceptor-ref>


如果有部分动作不需要此过滤器 就可以直接使用

  <action name="login" class="com.heima.action.DemoAction"  method="login">     <result type="redirectAction">goMain</result>     <interceptor-ref name="myDefault">     <param name="checkLogin.excludeMethods">login</param><!-- 这里要用过滤器的引用 和方法名 然后注入  方法 -->     </interceptor-ref>     </action>

   大概就是这样了   第一次学这个 还需要多多联系


  

0 0
原创粉丝点击