struts2 拦截器栈

当需要引用的拦截器过多时,可以把拦截器定义在一个拦截器栈里面,而在action中只引用这个拦截器栈,从而简化代码。拦截器栈可以引用另一个拦截器栈。

拦截器栈定义如下,而使用拦截器栈的语法与使用拦截器一致。

    <package name="default" namespace="/" extends="struts-default">
        <!-- 配置自定义拦截器 -->
        <interceptors>
            <interceptor name="myInterceptor" class="com.interceptor.MyInterceptor"></interceptor>

            <interceptor-stack name="myStack">
                <interceptor-ref name="myInterceptor"></interceptor-ref>
                <interceptor-ref name="token"></interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <!-- 在action中使用自定义拦截器 -->
        <action name="login" class="com.action.LoginAction" method="login">
            <result>/index.jsp</result>
            <result name="invalid.token">/token.jsp</result>

            <interceptor-ref name="myStack"></interceptor-ref>
            <!-- interceptor-ref name="defaultStack"></interceptor-ref -->
        </action>
    </package>

 

如果拦截器栈是默认都要引用的,那么可以定义默认拦截器栈,这样没有显示引用拦截器的action,都会默认引用默认拦截器栈。实际上struts-default 已经定义了默认拦截器栈。

 <package name="default" namespace="/" extends="struts-default">
        <!-- 配置自定义拦截器 -->
        <interceptors>
            <interceptor name="myInterceptor" class="com.interceptor.MyInterceptor"></interceptor>

            <interceptor-stack name="myStack">
                <interceptor-ref name="myInterceptor"></interceptor-ref>
                <interceptor-ref name="token"></interceptor-ref>
            </interceptor-stack>
        </interceptors>
        
        <!-- 定义默认拦截器栈 -->
        <default-interceptor-ref name="myStack"></default-interceptor-ref>

        <!-- 在action中使用自定义拦截器 -->
        <action name="login" class="com.action.LoginAction" method="login">
            <result>/index.jsp</result>
            <result name="invalid.token">/token.jsp</result>
            <!-- interceptor-ref name="myStack"></interceptor-ref -->
            <!-- interceptor-ref name="defaultStack"></interceptor-ref -->
        </action>
    </package>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值