自定义拦截器及拦截器栈
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
<package name="p1" extends="struts-default">
<interceptors>
<interceptor name="privilage" class="cn.qyc.interceptor.Inter"></interceptor>
<interceptor-stack name="mystack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="privilage"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="login" class="cn.qyc.loGIN.Login" method="login">
<result name="success" type="redirect">/book.jsp</result>
<result name="error" type="dispatcher">/error.jsp</result>
</action>
<action name="book_*" class="cn.qyc.book.BookseOperation" method="{1}">
<result name="success" type="redirect">/booksuccess.jsp</result>
<result name="login" type="redirect">/index.jsp</result>
<interceptor-ref name="mystack"></interceptor-ref>
</action>
</package>
</struts>
继承AbstractInterceptor
package cn.qyc.interceptor;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class Inter extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocations) throws Exception {
// TODO Auto-generated method stub
ActionContext context = invocations.getInvocationContext();
ActionContext context2 = ActionContext.getContext();
String usernameString = (String) context.getSession().get("username");
if (usernameString!=null) {
context2.getSession().put("msg", "已登陆");
return invocations.invoke();
}
else {
context2.getSession().put("msg","未登录");
System.out.println(context2.getSession().get("msg"));
return Action.LOGIN;
}
}
}