【 Struts2 过滤器】
LoginInterceptor
package k.util; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; import org.apache.struts2.ServletActionContext; import javax.servlet.http.HttpServletRequest; public class LoginInterceptor extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation actionInvocation) throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); Object name = request.getSession().getAttribute("name"); return name == null ? "toLogin" : actionInvocation.invoke(); } }
struts.xml
<?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> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="struts2-hibernate" extends="struts-default" namespace="/"> <interceptors> <interceptor name="LoginInterceptor" class="k.util.LoginInterceptor"> <param name="excludeMethods">toLogin,login</param> </interceptor> </interceptors> <action name="student_*" class="k.action.StudentAction" method="{1}"> <interceptor-ref name="LoginInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="list" type="redirectAction">student_list</result> <result name="toList">/WEB-INF/student/list.jsp</result> <result name="toAdd">/WEB-INF/student/add.jsp</result> <result name="toUpdate">/WEB-INF/student/update.jsp</result> <result name="toLogin">/WEB-INF/student/login.jsp</result> </action> </package> </struts>