struts2拦截器笔记

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


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

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

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


<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">
//此处引入了拦截器栈,需要进行拦截处理
<interceptor-ref name="mystack"></interceptor-ref> 

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



如需执行拦截器栈则必须在  public String intercept(ActionInvocation invoker)方法中

调用invocation.invoke()方法,否则只会执行第一个拦截器,后面的拦截器不会执行

猜你喜欢

转载自zhangzhi199129.iteye.com/blog/1593488