Struts2中自定义拦截器导致Action注入参数丢失解决

做一个登录验证的页面,对集成admin的所有package进行保护。

        <interceptors>
    <interceptor name="adminLoginInterceptor"
     class="interceptor.AdminLoginInterceptor" >
                    <param name="pass0">6B8E49836C0C29251833227E3B0F7FB7F8DC67CE</param>
                </interceptor>
   </interceptors>
        <default-interceptor-ref name="adminLoginInterceptor" />

然而发现所有带有传递参数的均无法正常使用了,在Action中所有的参数无法被注入。

最后发现,struts-default中,默认的拦截器引用是defaultstack,这个拦截器包传说是经过精心设计的。。所以会把所有的参数注入!。。。

因此要更改默认拦截器,需要加上这个defaultstack

    <package name="admin" extends="struts-default" >
        <interceptors>
    <interceptor name="adminLoginInterceptor"
     class="interceptor.AdminLoginInterceptor" >
                    <param name="pass0">6B8E49836C0C29251833227E3B0F7FB7F8DC67CE</param>
                </interceptor>
        <interceptor-stack name="adminstack">
                   <interceptor-ref name="adminLoginInterceptor"></interceptor-ref>
                   <interceptor-ref name="defaultStack"></interceptor-ref>
        </interceptor-stack>

   </interceptors>

        <default-interceptor-ref name="adminstack" />
        <global-results>
            <result name="login">/admin/login.jsp</result>
        </global-results>
    </package>
        <default-interceptor-ref name="adminstack" />
        <global-results>
            <result name="login">/admin/login.jsp</result>
        </global-results>
    </package>

特别注意红色两行的顺序!先自定义再Default

===================================以上是转自别人博客===============================

延伸到自己遇到的问题

场景:我自定义了一个拦截器,是为了拦截某个固定的action,所以没有放到默认拦截器栈中,添加到某个需要被拦截的action配置文件中。但是添加拦截器之后,在action中无法注入参数,通过get方法取不到了。

配置如下:

--------struts.xml-------

<interceptors>
      <interceptor name="sessionCheck" class="com.wac.common.web.interceptor.WebCookiesInterceptor" />
   <interceptor name="loginCheck" class="com.wac.common.web.interceptor.LoginInterceptor" />
        <interceptor name="loggingStack" class="com.wac.common.web.interceptor.LoggingInterceptor" />
        <interceptor name="inch" class="financeInterfInterceptor"/>
        <interceptor name="signAuthJSONInterceptor" class="com.wacai.common.web.interceptor.SignAuthJSONInterceptor"></interceptor>
        <interceptor name="signAuthHtmlInterceptor" class="com.wacai.common.web.interceptor.SignAuthHttpInterceptor"></interceptor>
        <interceptor-stack name="myStack">
               <interceptor-ref name="sessionCheck" />
               <interceptor-ref name="loginCheck" />
               <interceptor-ref name="loggingStack" />
               <interceptor-ref name="defaultStack" />
               <interceptor-ref name="inch" />
         </interceptor-stack>
  </interceptors>

  <default-interceptor-ref name="myStack" />

固定的需要拦截的action配置文件struts-manage.xml

<!-- 账户赠送forward -->
  <action name="send_gift" class="com.wacai.payment.management.channel.action.AssetManagementAction" method="doSendGift">
    <interceptor-ref name="signAuthJSONInterceptor"></interceptor-ref>
    <interceptor-ref name="defaultStack"/>
  </action>

 其中<interceptor-ref name="defaultStack"/>这段添加后,action中的参数就能获取了!

猜你喜欢

转载自hujinfan.iteye.com/blog/2166023