Struts2配置拦截器和拦截链
来源:互联网 发布:淘宝怎么申请退货 编辑:程序博客网 时间:2024/04/29 10:32
1.拦截器的配置
此处以“检测用户是否登录”为例,
MyInterceptor.Java
- public class MyInterceptor extends AbstractInterceptor {
- @Override
- public String intercept(ActionInvocation invocation) throws Exception {
- System.out.println("---------intercept()---------");
- Map<String, Object> session = invocation.getInvocationContext().getSession();
- if (session.get("username") != null) {
- return invocation.invoke();
- } else {
- return "checkError";
- }
- }
- }
struts.xml文件中配置:
- <package name="testLogin" namespace="/" extends="struts-default">
- <interceptors>
- <interceptor name="myInterceptor" class="MyInterceptor" />
- <!-- class="MyInterceptor"已在spring配置文件中配置bean -->
- </interceptors>
- <action name="user" class="UserManagerAction">
- <result name="success">/user.jsp</result>
- <result name="error" type="redirect">/error.jsp</result>
- <result name="checkError">/checkSession.jsp</result>
- <interceptor-ref name="myInterceptor" />
- <interceptor-ref name="defaultStack" />
- </action>
- </package>
注意:action中,设置好自己的interceptor后,别忘了配置defaultStack拦截器。
defaultStack是Struts2默认的拦截器,帮助我们完成许多struts2提供的服务。
2.拦截链的配置
当一个action需要多个拦截器时,我们可以配置拦截链
- <package name="testLogin" namespace="/" extends="struts-default">
- <interceptors>
- <interceptor name="myInterceptor" class="MyInterceptor" />
- <interceptor name="otherMyInterceptor" class="OtherMyInterceptor" />
- <!-- 已在spring的applicationContext-bean.xml中配置相关bean -->
- <interceptor-stack name="defaultInterceptor">
- <interceptor-ref name="myInterceptor" />
- <interceptor-ref name="defaultStack" />
- </interceptor-stack>
- <interceptor-stack name="otherDefaultInterceptor">
- <interceptor-ref name="otherMyInterceptor" />
- <interceptor-ref name="defaultStack" />
- </interceptor-stack>
- </interceptors>
- <action name="user" class="UserManagerAction">
- <result name="success">/user.jsp</result>
- <result name="error" type="redirect">/error.jsp</result>
- <result name="checkError">/checkSession.jsp</result>
- <interceptor-ref name="otherDefaultInterceptor" />
- </action>
- </package>
3.设置默认拦截器/拦截链(普遍配置)
当多个action都需要配置相同的拦截器/拦截链时,考虑设置默认的拦截器/拦截链,则不用在每个action中依次配置了。
- <package name="testLogin" namespace="/" extends="struts-default">
- <interceptors>
- <interceptor name="myInterceptor" class="MyInterceptor" />
- <interceptor name="otherMyInterceptor" class="OtherMyInterceptor" />
- <!-- 已在spring的applicationContext-bean.xml中配置相关bean -->
- <interceptor-stack name="defaultInterceptor">
- <interceptor-ref name="myInterceptor" />
- <interceptor-ref name="defaultStack" />
- </interceptor-stack>
- <interceptor-stack name="otherDefaultInterceptor">
- <interceptor-ref name="otherMyInterceptor" />
- <interceptor-ref name="defaultStack" />
- </interceptor-stack>
- </interceptors>
- <default-interceptor-ref name="defaultInterceptor" />
- <action name="user" class="UserManagerAction">
- <result name="success">/user.jsp</result>
- <result name="error" type="redirect">/error.jsp</result>
- <result name="checkError">/checkSession.jsp</result>
- </action>
- </package>
4.屏蔽默认拦截器/拦截链(特殊配置)
当所在package中设置了默认拦截器/拦截链,某些个别action需要特殊拦截器/拦截链时,单独在action中配置所需拦截器/拦截器,即可屏蔽默认拦截器/拦截链,执行单独设置的拦截器/拦截链。
- <package name="testLogin" namespace="/" extends="struts-default">
- <interceptors>
- <interceptor name="myInterceptor" class="MyInterceptor" />
- <interceptor name="otherMyInterceptor" class="OtherMyInterceptor" />
- <!-- 已在spring的applicationContext-bean.xml中配置相关bean -->
- <interceptor-stack name="defaultInterceptor">
- <interceptor-ref name="myInterceptor" />
- <interceptor-ref name="defaultStack" />
- </interceptor-stack>
- <interceptor-stack name="otherDefaultInterceptor">
- <interceptor-ref name="otherMyInterceptor" />
- <interceptor-ref name="defaultStack" />
- </interceptor-stack>
- </interceptors>
- <default-interceptor-ref name="defaultInterceptor" />
- <action name="user" class="UserManagerAction">
- <result name="success">/user.jsp</result>
- <result name="error" type="redirect">/error.jsp</result>
- <result name="checkError">/checkSession.jsp</result>
- <interceptor-ref name="otherDefaultInterceptor" />
- </action>
- </package>
0 0
- Struts2配置拦截器和拦截链
- Struts2配置拦截器和拦截链
- Struts2拦截器配置
- struts2 拦截器配置
- Struts2配置拦截器
- 配置struts2拦截器
- Struts2拦截器配置
- struts2 拦截器配置
- Struts2拦截器配置
- struts2拦截器配置
- struts2 拦截器配置
- Struts2拦截器配置
- Struts2拦截器配置
- struts2拦截器配置
- Struts2拦截器配置
- Struts2 拦截器配置
- Struts2 自定义拦截器配置和使用
- struts2的自定义拦截器和配置
- FIONBIO was not declared in this scope
- C语言 gets()和scanf()函数的区别
- [已解决]Failed to resolve:com.adnroid.support:design:23.4.0
- CSS3实现头像旋转功能
- CUDA之学习资料
- Struts2配置拦截器和拦截链
- Leetcode 198. House Robber & 213. House Robber II & 337. House Robber III
- Hadoop之——windows7+eclipse+hadoop2.5.2环境配置
- 游惠Pro技术支持
- 相似图片搜索的原理
- 【Android测试】【第七节】Monkey——源码浅谈
- 条件式函数声明:丧失函数声明提升能力
- 像素长宽比4:3与16:9的问题
- 拳王康斯坦丁(中)