定义拦截器有2种办法:
1.实现Interceptor接口
2.集成AbstractInterceptor抽象类
一、方法1
.....
<struts>
<package name="mypack" extends="struts-default">
<!--i. j. k.-->
<interceptors>
<interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
<global-results>
<result name="login" type="redirectAction">userAction_loginView</result>
</global-results>
<!--不写method,111默认就是execute-->
<action name="indexAction" class="com.nf.action.IndexAction">
<result name="success">/WEB-INF/jsp/index.jsp</result>
<interceptor-ref name="myStack"></interceptor-ref>
<!--
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
-->
</action>
<action ......
</action>
</package>
</struts>
package com.nf.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; import java.util.Map; /* 定义拦截器有2种办法: 1.实现Interceptor接口 2.集成AbstractInterceptor抽象类 */ public class LoginInterceptor implements Interceptor { private Map<String,Object> session = null; public void destroy() { } public void init() { } public String intercept(ActionInvocation actionInvocation) throws Exception { Object myAction = actionInvocation.getAction(); if(myAction instanceof UserAction){ System.out.println("你访问的Action是UserAction,不要校验Session,否则死循环"); //放行 return actionInvocation.invoke(); }else{ System.out.println("你访问的Action是:"+myAction); } session = ActionContext.getContext().getSession(); Object user = session.get("user"); if (user!=null){ return actionInvocation.invoke(); }else{ return "login"; } } }
二、方法2
package com.nf.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; import java.util.Map; /* 定义拦截器有2种办法: 1.实现Interceptor接口 2.集成AbstractInterceptor抽象类 */ public class LoginInterceptor extends MethodFilterInterceptor { private Map<String, Object> session = null; protected String doIntercept(ActionInvocation actionInvocation) throws Exception { session = ActionContext.getContext().getSession(); Object user = session.get("user"); if (user!=null){ return actionInvocation.invoke(); }else{ return "login"; } } }
......
<struts>
<package name="mypack" extends="struts-default">
<!--i. j. k.-->
<interceptors>
<interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor">
<!--excludeMethods需要生效的话,
自定义的拦截器,不能使用实现Interceptor接口,
而是extends MethodFilterInterceptor
-->
<param name="excludeMethods">loginView,login</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
<global-results>
<result name="login" type="redirectAction">userAction_loginView</result>
</global-results>
<!--不写method,111默认就是execute-->
<action name="indexAction" class="com.nf.action.IndexAction">
<result name="success">/WEB-INF/jsp/index.jsp</result>
<!-- <interceptor-ref name="myStack"></interceptor-ref> -->
<!--
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
-->
</action>
<action name="therFunctionAction" class="com.nf.action.OtherFunctionAction">
<!--不写name,222默认就是success-->
<result>/WEB-INF/jsp/otherFunction.jsp</result>
</action>
<action.....
</action>
</package>
</struts>