博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
拦截器
阅读量:4580 次
发布时间:2019-06-09

本文共 1659 字,大约阅读时间需要 5 分钟。

  xwork包-->interceter下的每一个类进行一个拦截的拦截器

       

 

  在配置文件中,配置拦截器

                < package>

                     <interceptor name="" class=""/>

                     <action>

                      <interceter-ref name=""/>

                     </action>

                 </package>

 

说明,当package extends /struts-default 包的时候,就引用到了struts2在struts-default.xml中为我们声明的很多的拦截器,我们要用就无需再声明,可以直接在action中引用,另外在该文件中还为我们将strutsStack拦截器栈中的拦截器自动引用到了action中,但是如果我们又自己引用了拦截器,那么默认拦截器栈就不引用了,如果我们还想要用到(基本上都要用到),我们可以假配置进去<interceter-ref name="defaultStack" />

 

自定义拦截器

    1.实现Interceptor接口或者继承自AbsctractInterceptor 重写intercetor方法

         

public class AuthIntercepter extends AbstractInterceptor {    @Override    public String intercept(ActionInvocation invocation) throws Exception {        Map session = ActionContext.getContext().getSession();        StuInfo stuinfo = (StuInfo) session.get("user");        if(null!=stuinfo){            //跳转到正常的页面            return invocation.invoke();//表示继续程序继续往下执行              /**               *     String result = invocation.invoke();               *        ......//表示执行完Action后来到这个拦截器做的事情               **/
}else{ //跳转到登陆页面 return "login"; } } }

 

 

 

          配置文件写法

                <package>

                    <intercetors>

                       <interceptor name="interceptorName" class="class path"/>

                    </intercetors>

                    <action>

                       <interceptor-ref   />

                    </action>

               </package>

 

 2.继承自MethodFileter Intercrptor(表示只拦截Action中的一些方法)

             protect String doInterceptor(ActionInvocation invoke){

                      //事件处理

               }

     配置文件

            <interceptors>

                 <interceptor name="" class="" >

                  写法A: <param name="excludeMethodms" >method1,method2</param> 

                  写法B: <param name="includeMethodms" >method1,method2</param> 

                </interceptor>

             </interceptors>

 如果excludeMethodms和includeMethodms都包含了m1方法 includeMethod的优先级高,如果都是includeMethod或者都是excludeMethods采用就近原则

 

 

转载于:https://www.cnblogs.com/weidan/archive/2013/01/26/2877758.html

你可能感兴趣的文章