struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解
2014-09-22 11:15
423 查看
参考: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;
}
只需要实现该类中的
即可。
样例代码:
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就可以起到作用了
通过对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就可以起到作用了
相关文章推荐
- struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解
- struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解
- struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解
- struts2中关于拦截器Interceptor中的excludeMethods和includeMethods的理解
- struts2中关于拦截器Interceptor中的excludeMethods和includeMethods
- Struts2拦截器属性excludeMethods、includeMethods配置无效之解决方法
- Struts2拦截器属性excludeMethods、includeMethods配置无效之解决方法
- struts2拦截器interceptor的三种配置方法
- struts2拦截器interceptor的三种配置方法
- struts2拦截器interceptor的三种配置方法
- Struts2的Interceptor中使用includeMethods 和 excludeMethods
- struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证
- Struts2拦截器Interceptor执行顺序理解
- Struts2使用MethodFilterInterceptor来实现方法拦截器
- struts2 Interceptor(拦截器)中获取HTTP 参数的方法
- Struts2——MethodFilterInterceptor--方法拦截器
- struts2拦截器interceptor的三种配置方法
- Struts2 的 Preparable, ModelDriven 接口方法里无法获得参数,理解 拦截器与责任链
- Struts2拦截器属性excludeMethods、includeMethods配置无效的原因
- struts2拦截器interceptor的三种配置方法