struts2中方法攔截器(Interceptor)的中的excludeMethods與includeMethods的理解


通過對struts2的學習,對於interceptor中的excludeMethods與includeMethods的理解:

針對MethodFilterInterceptor:

excludeMethods表示排除指定的方法,即不對標記為excludeMethods的方法進行攔截,

includeMethods表示包含指定的方法,即對標記為includeMethods的方法進行攔截,

 

在struts.xml中關於excludeMethods和includeMethods有兩種實現方式,一種相當於全局,另一種相當於局部,即

<interceptors>
<interceptor name="method" class="com.yxl.interceptor.MethodInterceptor">
<param name="includeMethods">method1,method2</param>
</interceptor>
</interceptors>

為全局。

<interceptor-ref name="method">
<param name="excludeMethods">method1,method2</param>
</interceptor-ref> 

為局部,

若全局中的param定義為excludeMethods同樣局部中的param也定義為excludeMethods,則局部中的param生效,全局中的param無效,即被局部中的param覆蓋,同樣,若全局中的param定義為includeMethods同樣局部中的param也定義為includeMethods,則局部中的param生效,全局中的param無效,即被局部中的param覆蓋。

當全局中的param與局部中的param不相同的時,即當全局中param為excludeMethods而局部中的param為includeMethods或全局中的param為includeMethods而局部中param為excludeMethods,則標志為includeMethods生效。即若是全局中的param定義為includeMethods,則全局屏蔽局部,以全局為准,反之,若局部的param定義為includeMethods,則以局部為准。

如果即沒有指定includeMethods也沒有指定excludeMethods的方法默認的時includeMethos方法

如果僅僅指定了includeMethods的方法,沒有包含在includeMethods里的方法就不會被攔截。

 

 要實現自定義攔截器,需要繼承MethodFilterInterceptor類。MethodFilterInterceptor類是AbstractInterceptor的子類,其源代碼如下:

package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.util.TextParseUtil;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

import java.util.Collections;
import java.util.Set;

public abstract class MethodFilterInterceptor extends AbstractInterceptor {
    protected transient Logger log = LoggerFactory.getLogger(getClass());
    
    protected Set<String> excludeMethods = Collections.emptySet();
    protected Set<String> includeMethods = Collections.emptySet();

    public void setExcludeMethods(String excludeMethods) {
        this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);
    }
    
    public Set<String> getExcludeMethodsSet() {
        return excludeMethods;
    }

    public void setIncludeMethods(String includeMethods) {
        this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods);
    }
    
    public Set<String> getIncludeMethodsSet() {
        return includeMethods;
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        if (applyInterceptor(invocation)) {
            return doIntercept(invocation);
        } 
        return invocation.invoke();
    }

    protected boolean applyInterceptor(ActionInvocation invocation) {
        String method = invocation.getProxy().getMethod();
        // ValidationInterceptor
        boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method);
        if (log.isDebugEnabled()) {
            if (!applyMethod) {
                log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list.");
            }
        }
        return applyMethod;
    }
    
    /**
     * Subclasses must override to implement the interceptor logic.
     * 
     * @param invocation the action invocation
     * @return the result of invocation
     * @throws Exception
     */
    protected abstract String doIntercept(ActionInvocation invocation) throws Exception;
    
}


只需要實現該類中的如下方法即可

 

protected abstract String doIntercept(ActionInvocation invocation) throws Exception 

 

樣例代碼:
package cua.survey.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class LoginInterceptor extends MethodFilterInterceptor{

    private static final long serialVersionUID = 1L;

    protected String doIntercept(ActionInvocation action) throws Exception {
        Map<String, Object> session = ActionContext.getContext().getSession();
        String user = (String)session.get("user");
        if(user != null && !"".equals(user)){
//通過
            return action.invoke();
        }else{
//不通過,報錯
            session.put("error", "your user or pwd is error, please login again...");
            return Action.LOGIN;
        }

    }

}

  

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

相關:

攔截器的基本使用方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM