1. 程式人生 > >Struts2攔截器屬性excludeMethods、includeMethods配置無效之解決方法

Struts2攔截器屬性excludeMethods、includeMethods配置無效之解決方法

參考:http://blog.csdn.net/coolcoffee168/article/details/7963251

 在配置struts2 攔截器屬性excludeMethods、includeMethods進行方法過濾時發現不起作用。

       經過檢視書籍之後發現,要想使方法過濾配置起作用,攔截器需要繼承MethodFilterInterceptor類。MethodFilterInterceptor類是AbstractInterceptor的子類,其原始碼如下:

  1. public abstract class MethodFilterInterceptor extends AbstractInterceptor {  
  2.     protected transient Logger log = LoggerFactory.getLogger(getClass());  
  3.     protected Set<String> excludeMethods = Collections.emptySet();  
  4.     protected Set<String> includeMethods = Collections.emptySet();  
  5.     public void setExcludeMethods(String excludeMethods) {  
  6.         this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);  
  7.     }  
  8.     public Set<String> getExcludeMethodsSet() {  
  9.         return excludeMethods;  
  10.     }  
  11.     public void setIncludeMethods(String includeMethods) {  
  12.         this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods);  
  13.     }  
  14.     public Set<String> getIncludeMethodsSet() {  
  15.         return includeMethods;  
  16.     }  
  17.     @Override  
  18.     public String intercept(ActionInvocation invocation) throws Exception {  
  19.         if (applyInterceptor(invocation)) {  
  20.             return doIntercept(invocation);  
  21.         }   
  22.         return invocation.invoke();  
  23.     }  
  24.     protected boolean applyInterceptor(ActionInvocation invocation) {  
  25.         String method = invocation.getProxy().getMethod();  
  26.         // ValidationInterceptor  
  27.         boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method);  
  28.         if (log.isDebugEnabled()) {  
  29.             if (!applyMethod) {  
  30.                 log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list.");  
  31.             }  
  32.         }  
  33.         return applyMethod;  
  34.     }  
  35.     /** 
  36.      * Subclasses must override to implement the interceptor logic. 
  37.      *  
  38.      * @param invocation the action invocation 
  39.      * @return the result of invocation 
  40.      * @throws Exception 
  41.      */  
  42.     protected abstract String doIntercept(ActionInvocation invocation) throws Exception;  
  43. }  

只需要實現該類中的
  1. protected abstract String doIntercept(ActionInvocation invocation) throws Exception  
即可。

樣例程式碼:

  1. package cua.survey.interceptor;  
  2. import java.util.Map;  
  3. import com.opensymphony.xwork2.Action;  
  4. import com.opensymphony.xwork2.ActionContext;  
  5. import com.opensymphony.xwork2.ActionInvocation;  
  6. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  7. import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;  
  8. public class LoginInterceptor extends MethodFilterInterceptor{  
  9.     private static final long serialVersionUID = 1L;  
  10.     protected String doIntercept(ActionInvocation action) throws Exception {  
  11.         Map<String, Object> session = ActionContext.getContext().getSession();  
  12.         String user = (String)session.get("user");  
  13.         if(user != null && !"".equals(user)){  
  14.             return action.invoke();  
  15.         }else{  
  16.             session.put("error""your user or pwd is error, please login again...");  
  17.             return Action.LOGIN;  
  18.         }  
  19.     }  
  20. }  

實現之後攔截器屬性excludeMethods、includeMethods就可以起到作用了。