struts2 拦截器

just posted @ 2009年3月27日 07:29 in struts2 with tags 拦截器 , 1430 阅读

如果struts.xml的action里配置了拦截器 则默认不加载struts-default.xml里的拦截器
如果需要则要手工加入:

 

<action name="register" class="com.test.action.RegisterAction">
        <result>/success.jsp</result>
        <result name="input">/register.jsp</result>
       
        <!-- 拦截器 -->
        <interceptor-ref name="myInterceptor"></interceptor-ref>
        <!-- 默认拦截器 -->
        <interceptor-ref name="defaultStack"></interceptor-ref>
      </action>

 


给拦截器传递参数:方法一

 

<interceptors>
       <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
        <param name="hello">world</param>
       </interceptor>
</interceptors>

然后再MyInterceptor.java里定义

 

private String hello;
       
        public String getHello() {
                return hello;
        }

        public void setHello(String hello) {
                this.hello = hello;
        }

在init()里启动时就可以看到

 

public void init() {
                // TODO Auto-generated method stub
                System.out.println("init............");
                System.out.println("hello............"+getHello());
        }

给拦截器传递参数:方法二:在action 里传递参数

 

<action name="register" class="com.test.action.RegisterAction">
        <result>/success.jsp</result>
        <result name="input">/register.jsp</result>
       
        <!-- 拦截器 -->
        <interceptor-ref name="myInterceptor">
           <param name="hello">welcome</param>
        </interceptor-ref>
        <!-- 默认拦截器 -->
        <interceptor-ref name="defaultStack"></interceptor-ref>
      </action>

启动是显示 welcome  会覆盖上面的那个 world

 


拦截器栈

 

<interceptor-stack name="myStack">
       <!-- 按顺序执行  先配置的先执行 最后结束  (先进后出):先执行myInterceptor,在执行myInterceptor2 然后 myInterceptor2结束 ,
           最后 myInterceptor结束-->

           
          <interceptor-ref name="myInterceptor"></interceptor-ref>
          <interceptor-ref name="myInterceptor2"></interceptor-ref>
          <interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>

以上拦截所有的action方法

 


下面  方法过滤拦截器  MethodFilterInterceptor() 拦截某个具体的方法

拦截器要继承 MethodFilterInterceptor 并实现doIntercept(ActionInvocation invocation)

 

<action name="register" class="com.test.action.RegisterAction" method="test">
        <result>/success.jsp</result>
        <result name="input">/register.jsp</result>
       
        <interceptor-ref name="myInterceptor3">
        <!-- 拦截RegisterAction 的test 和execute 方法-->
                <param name="includeMethods">test,execute</param>
               
         <!-- excludeMethods 排除 -->
                <param name="excludeMethods">test,execute</param>
        </interceptor-ref>
       
        <!--defaultStack 放在后面否则不会执行myInterceptor3 注意顺序-->
        <interceptor-ref name="defaultStack"></interceptor-ref>
      </action>

includeMethods  优先级要高于 excludeMethods 。

 

 

 

 

 

 

 

  • 无匹配
NCERT Urdu Question 说:
2022年9月29日 01:13

Urdu prose and poetry have a lot of work in history. When kids learn it, they get to learn a lot about our history. Our kids must learn about our rich history. To be able to grasp the present, NCERT Urdu Question Paper Class 11 we must know the past well.Urdu learning can help kids gain a lot of cultural knowledge. The work of Urdu prose and poetry is the best source for the kids to learn about our values and norms.Urdu prose and poetry have a lot of work in history. When kids learn it, they get to learn a lot about our history.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter