1、定义拦截器,继承MethodFilterInterceptor
package com.life.stuts.interceptor;import java.util.Map;import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;/*** 如果不是login的adction,使用自定义拦截器判断用户是否为空。为空,跳转到登陆页面;否则,继续执行。* 在配置拦截器属性excludeMethods、includeMethods进行方法过滤时发现不起作用。* 要想使方法过滤配置起作用,拦截器需要继承MethodFilterInterceptor类。* MethodFilterInterceptor类是AbstractInterceptor的子类* @author JL* */public class SellerInterceptor extends MethodFilterInterceptor {/*** */private static final long serialVersionUID = 1L;@Overrideprotected String doIntercept(ActionInvocation invocation) throws Exception {// TODO Auto-generated method stubMap<String, Object> sessionValues = invocation.getInvocationContext().getSession();String seller = (String) sessionValues.get("seller");// 如果没有管理员登陆或商家登陆,就返回到登陆页面,否则继续访问原actionSystem.out.println("action拦截器");if (seller == null) {return "loginscript";}return invocation.invoke();}}
2、struts.xml的配置,希望对那个action进行拦截就把interceptor 放里面,要加上defaultStack。
<interceptors><interceptor name="authentication"class="com.life.stuts.interceptor.AuthenticationInterceptor">interceptor>interceptors> <global-results><result name="login">/result>global-results> <action name="*_manage" method="{1}" class="ManageAction"><interceptor-ref name="authentication">interceptor-ref><interceptor-ref name="defaultStack">interceptor-ref>action>
3、实现对action的某些方法不拦截
excludeMethods表示不进行拦截
includeMethods表示要拦截
<interceptor-ref name="sellerauthentication"><param name="includeMethods">param><param name="excludeMethods">login,logoutparam>interceptor-ref>
Done