struts2拦截器笔记

[b]如果用户要开发自己的拦截类,需要实现Interceptor接口,它包括三个方法:[/b][color=red]
Init()
destroy()
intercept(ActionInvocation action)[/color]

另一种只需继承AbstractInterceptor类实现 AbstractInterceptor方法

拦截器在strut2中配置的详细解析

<package name="default" extends="struts-default" namespace="/">
<interceptors>
[color=red]//自定义的一个拦截器 name为 login_authority, class为拦截器的处理的类名[/color]
<interceptor name="login_authority"class="com.TestOfAuthorityInterceptor">
//拦截器中初始的参数
<param name="user">lisi</param></interceptor>
[color=red]//自定义的一个拦截器 name为 zz_test, class为拦截器的处理的类名[/color]
<interceptor name="zz_test" class="com.TestOfAuthorityInterceptor">
//拦截器中初始的参数
<param name="user">zhangsan</param></interceptor>
//以下是拦截器栈,name为拦截器栈的名称
<interceptor-stack name="mystack">
//引入默认的拦截器栈
<interceptor-ref name="defaultStack"></interceptor-ref>
[color=red]//引入自定义的拦截器栈 login_authority 拦截器栈按顺序执行[/color]
<interceptor-ref name="login_authority"></interceptor-ref>
//引入自定义的拦截器栈 zz_test
<interceptor-ref name="zz_test"></interceptor-ref>
</interceptor-stack>
</interceptors>
[color=red]//如果需要将自定义的拦截器栈定义为默认的拦截器栈,只需要在此处代码加入 mystack为自定义的拦截器栈
//<default-interceptor-ref name="mystack" /> [/color]

<global-results>
<result name="authorizeFailed">/authorizeFailed.jsp</result>
<result name="zs">/zhangsan.jsp</result>
<result name="ls">/lisiUser.jsp</result>
</global-results>

<action name="demo" class="com.Demo">
<interceptor-ref name="timer"></interceptor-ref>
<result name="success">/ok.jsp</result>
</action>
<action name="reg" class="com.Reg">
<interceptor-ref name="token"></interceptor-ref>
<result name="invalid.token">/regerr.jsp</result>
<result name="success">/regok.jsp</result>
</action>

<action name="user_*" method="{1}" class="com.UserAction">
[color=red]//此处引入了拦截器栈,需要进行拦截处理
<interceptor-ref name="mystack"></interceptor-ref> [/color]

<result name="success">/index.jsp</result>
<result name="input">/authorizeFailed.jsp</result>
</action>
</package>


[color=red][b]如需执行拦截器栈则必须在 public String intercept(ActionInvocation invoker)方法中

调用invocation.invoke()方法,否则只会执行第一个拦截器,后面的拦截器不会执行[/b][/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值