struts2 配置
<package name="admin" extends="struts-default" namespace="/"> <interceptors> <interceptor name="loginInterceptor" class="com.sec.Interceptor.LoginInterceptor" /> <interceptor-stack name="checkStack"> <interceptor-ref name="loginInterceptor" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <action name="user" class="com.sec.action.UserAction"> <result name="main">main.jsp</result> <result name="list">userListView.jsp</result> <result name="update_list">updateUser.jsp</result> <result name="input">index.jsp</result> <interceptor-ref name="checkStack"></interceptor-ref> </action> </package>
自定义拦截器
package com.sec.Interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; import com.sec.action.UserAction; public class LoginInterceptor implements Interceptor{ @Override public void destroy() { // TODO Auto-generated method stub } @Override public void init() { // TODO Auto-generated method stub } @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("进入拦截器"); // 对LoginAction不做该项拦截 Object action = invocation.getAction(); if (action instanceof UserAction) { System.out.println("exit check login, because this is login action."); return invocation.invoke(); } return invocation.invoke(); } }
附上官方文档
http://struts.apache.org/docs/interceptors.html?
http://struts.apache.org/docs/how-do-we-configure-an-interceptor-to-be-used-with-every-action.html(这里写怎么配置的例子)