struts2的拦截器配置详解
2010-06-06 11:28
330 查看
1) 自定义拦截器(实现Interceptor接口或者继承AbstractInterceptor类);
2) 在struts.xml配置文件中注册拦截器,并在Action中引用
public class AuthorityInterceptor extends MethodFilterInterceptor{ @Override public String doIntercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub invocation.getAction().toString(); ActionContext actionContext = invocation.getInvocationContext(); Map session = actionContext.getSession(); User user = (User)session.get("user"); if(user==null) { return "login"; }else { System.out.println("这是拦截前======"); String result = invocation.invoke(); System.out.println(result); System.out.println("这是拦截后======"); return result; } } }
2) 在struts.xml配置文件中注册拦截器,并在Action中引用
<package name="system" extends="struts-default" namespace="/system"> <!-- 配置拦截器 --> <interceptors> <!-- 配置自定义拦截器 --> <interceptor name="authorityinterceptor" class="com.softfz.interceptor.AuthorityInterceptor"> <!-- 配置该拦截器不拦截的action方法名,即黑名单--> <param name="excludeMethods">login</param> <!-- 配置该拦截器拦截的action方法名,即白名单 <param name="includeMethods">XXX,XX</param> --> </interceptor> <!-- 配置自定义拦截器栈,注意自定义拦截器一定要包含【defaultStack】 拦截器的执行顺序:如果有拦截器a,b,c,则执行顺序为a-b-c-action-c-b-a --> <interceptor-stack name="mystack"> <interceptor-ref name="authorityinterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 配置默认拦截器 1.覆盖了在struts-default中的默认拦截器栈配置 2.即所有action都会先被默认拦截器所拦截 --> <default-interceptor-ref name="mystack" ></default-interceptor-ref> <action name="user" class="com.softfz.system.action.UserAction"> <result name="success">/welcome.jsp</result> <result name="login">/login.jsp</result> <result name="addBefore">/user_add.jsp</result> <!-- 可以单独为某个action配置拦截器 <interceptor-ref name=""></interceptor-ref> --> </action> </package>
相关文章推荐
- Struts2配置文件、拦截器详解
- Struts2拦截器Interceptor的原理与配置实例详解
- Struts2配置文件、拦截器等详解
- Struts2学习笔记(五)拦截器下 struts2拦截器配置及详解
- Struts2拦截器的使用(详解) ----全局配置
- Struts2拦截器Interceptor的原理与配置实例详解(十一)
- struts2拦截器配置详解
- Struts2中为什么要使用拦截器?拦截器的使用原理以及如何配置拦截器。
- struts2 18拦截器详解(十九)
- struts2 18拦截器详解(二十)
- Struts2配置文件详解——result type
- Struts2配置详解_配置Action
- 配置struts2默认的拦截器
- struts2的struts.properties配置文件详解
- eclipse Maven配置struts2详解——总结网上教程
- Struts2配置文件详解——struts.xml
- struts2拦截器配置
- STRUTS2.x 初始化相关的配置信息文件及struts.properties详解
- Struts2 拦截器流程图详解
- struts2注解下的拦截器配置