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

只需要实现该类中的
  1. protected abstract String doIntercept(ActionInvocation invocation) throws Exception  
即可。


样例代码:

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

实现之后 拦截器属性excludeMethods、includeMethods就可以起到作用了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值