spring拦截器配置

1.

拦截器的实现分3个步骤:

1,定义实现上述接口的拦截器类(com.opensymphony.xwork2.interceptor.Interceptor 或者继承AbstractInterceptor

2,在struts.xml中定义这个拦截器

3,在需要使用拦截器的Action中引用拦截器

2.

  代码:

public class LoginInterceptor implements Interceptor{
	private static final long serialVersionUID = 1L;

	public void destroy() {
	}

	public void init() {
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("进入拦截器");(拦截器相关的操作)
		 String re = invocation.invoke();
		return null;
	}
在Struts中配置:

<interceptors>
     <interceptor name="actionIntercept" class="interceptor.LoginInterceptor"></interceptor>
    </interceptors>

同时可以拦截器栈进行一组拦截器的配置:

 <interceptor-stack name="systemInterceptorStack">
    <interceptor-ref name="userAuthorityInterceptor" />
    <interceptor-ref name="exceptionInterceptor" />
   </interceptor-stack>

此放在<interceptors></interceptors>之间,在所含有的拦截器之后。

拦截器的配置范围:

1.某一个action中

   <action name="login" class="login" method="login">
      <result name="success">success.jsp</result>
      <interceptor-ref name="actionIntercept"></interceptor-ref>
      <interceptor-ref name="defaultStack"></interceptor-ref>  如果使用拦截器则必须添加默认的拦截器
    </action>

2.全局拦截器

将拦截器定义在一个包下:

<package name="all" extends="struts-default">
        <interceptors>
            <!-- 定义权限控制拦截器 -->
            <interceptor name="authority"
                class="akai.cost.ms.base.AuthInterceptor" />
            <!-- 定义一个包含权限控制的拦截器栈 -->
            <interceptor-stack name="mydefault">
                <interceptor-ref name="defaultStack" />
                <interceptor-ref name="authority" />
            </interceptor-stack>
        </interceptors>
        <!-- 定义默认拦截器 -->
        <default-interceptor-ref name="mydefault" />(包下的拦截器)

</package>

其他的包继承这个包:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值