struts2.0拦截器的用法:
配置文件:
<package name="default" extends="struts-default">
<interceptors>
<!-- 定义拦截器 loginInterceptor 继承default的package中action都可以引用该拦截器-->
<interceptor name="loginInterceptor" class="com.cetc.util.CheckLoginInterceptor" />
<!-- 定义拦截器堆栈 loginInterceptorStack -->
<interceptor-stack name="loginInterceptorStack">
<!-- 引用默认拦截器 defaultStack -->
<interceptor-ref name="defaultStack" />
<!-- 引用自定义拦截器 loginInterceptor -->
<interceptor-ref name="loginInterceptor" />
</interceptor-stack>
</interceptors>
<!-- 重定义默认拦截器堆栈 这里不要使用,若定义后凡是继承了default的package都回执行该拦截器-->
<!--<default-interceptor-ref name="loginInterceptorStack"/>-->
</package>
<package name="login" extends="default">
<action name="login" class="com.cetc.struts.action.t00hy.LoginAction" method="login">
<result name="input">/pages/home/login.jsp</result>
<result name="to_zbr">/pages/t02qy/util/index_zbr.jsp</result>
<result name="to_tbr">/pages/t02qy/util/index_tbr.jsp</result>
<result name="to_zj">/pages/t01zj/util/index_zj.jsp</result>
<result name="to_admin">/pages/home/index_admin.jsp</result>
<!-- 引用拦截器 loginInterceptorStack -->
<interceptor-ref name="loginInterceptorStack"></interceptor-ref>
</action>
<action name="qyLogin" class="com.cetc.struts.action.t00hy.LoginAction" method="qyLogin">
<result name="input">/pages/home/login.jsp</result>
<result name="to_zbr">/pages/t02qy/util/index_zbr.jsp</result>
<result name="to_tbr">/pages/t02qy/util/index_tbr.jsp</result>
<result name="to_zj">/pages/t01zj/util/index_zj.jsp</result>
<result name="to_admin">/pages/home/index_admin.jsp</result>
<!-- 引用拦截器 loginInterceptorStack -->
<interceptor-ref name="loginInterceptorStack"></interceptor-ref>
</action>
<action name="logout" class="com.cetc.struts.action.t00hy.LoginAction" method="logout">
<result name="success">/pages/home/login.jsp</result>
</action>
<action name="goLogin" class="com.cetc.struts.action.t00hy.LoginAction" method="goLogin">
<result name="success">/pages/home/login.jsp</result>
</action>
</package>
<package name="t00hy" namespace="/t00hy" extends="default">
<action name="updateTWzHyzh" class="com.cetc.struts.action.t00hy.TWzHyzhAction" method="updateTWzHyzh">
<result name="success" type="dispatcher">/pages/home/modifySuccess.jsp</result>
<result name="input" type="dispatcher">/pages/home/modify_password.jsp</result>
<!-- 引用拦截器 loginInterceptorStack -->
<interceptor-ref name="loginInterceptorStack"></interceptor-ref>
</action>
<action name="goModifyPassword" class="com.cetc.struts.action.t00hy.TWzHyzhAction" method="goModifyPassword">
<result name="success" type="dispatcher">/pages/home/modify_password.jsp</result>
<!-- 引用拦截器 loginInterceptorStack -->
<interceptor-ref name="loginInterceptorStack"></interceptor-ref>
</action>
</package>
抽象类:
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.ActionInvocation;
public abstract class AbstractInterceptor implements Interceptor {
public void destroy() {
// TODO Auto-generated method stub
System.out.println("LoginInterceptor.destroy()!");
}
public void init() {
// TODO Auto-generated method stub
System.out.println("LoginInterceptor.init()!");
}
public abstract String intercept(ActionInvocation invocation) throws Exception;
// TODO Auto-generated method stub
}
自定义拦截器:
import java.util.Map;
import com.cetc.struts.action.t00hy.LoginAction;
import com.opensymphony.xwork2.ActionInvocation;
public class CheckLoginInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -8624767071049356602L;
public static final String LOGIN_KEY = "LOGINED";
public static final String LOGIN_PAGE = "login";
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("begin check login interceptor!");
// 对LoginAction不做该项拦截
Object action = actionInvocation.getAction();
if (action instanceof LoginAction) {
System.out.println("exit check login, because this is login action.");
return actionInvocation.invoke();
}
// 确认Session中是否存在LOGIN
Map session = actionInvocation.getInvocationContext().getSession();
String login = (String) session.get(LOGIN_KEY);
if (login != null && login.length() > 0) {
// 存在的情况下进行后续操作。
System.out.println("already login!");
return actionInvocation.invoke();
}else{
// 否则终止后续操作,返回LOGIN
System.out.println("no login, forward login page!");
return LOGIN_PAGE;
}
}
}