参考:http://www.cnblogs.com/langtianya/archive/2013/04/10/3012205.html
通过对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,则全局屏蔽局部,以全局为准,反之,以局部为准。
要实现自定义拦截器,需要继承MethodFilterInterceptor类。MethodFilterInterceptor类是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;
- }
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
即可。
样例代码:
- 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;
- }
- }
- }
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就可以起到作用了