Struts2拦截器属性excludeMethods、includeMethods配置无效之解决方法
2014-09-24 21:10
429 查看
参考:http://blog.csdn.net/coolcoffee168/article/details/7963251
在配置struts2 拦截器属性excludeMethods、includeMethods进行方法过滤时发现不起作用。
经过查看书籍之后发现,要想使方法过滤配置起作用,拦截器需要继承MethodFilterInterceptor类。MethodFilterInterceptor类是AbstractInterceptor的子类,其源代码如下:
[java]
view plaincopy
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;
}
只需要实现该类中的
[java]
view plaincopy
protected abstract String doIntercept(ActionInvocation invocation) throws Exception
即可。
样例代码:
[java]
view plaincopy
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 拦截器属性excludeMethods、includeMethods进行方法过滤时发现不起作用。
经过查看书籍之后发现,要想使方法过滤配置起作用,拦截器需要继承MethodFilterInterceptor类。MethodFilterInterceptor类是AbstractInterceptor的子类,其源代码如下:
[java]
view plaincopy
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;
}
只需要实现该类中的
[java]
view plaincopy
protected abstract String doIntercept(ActionInvocation invocation) throws Exception
即可。
样例代码:
[java]
view plaincopy
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拦截器属性excludeMethods、includeMethods配置无效之解决方法
- Struts2拦截器属性excludeMethods、includeMethods配置无效的原因
- 自定义拦截器 includeMethods和excludeMethods无效 解决方法
- struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解
- struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解
- struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解
- 无法识别的属性“type”+IIS没有Asp.net配置选项的解决方法(转)
- Response.Redirect无效的解决方法(ajax配置文件)
- DCOM配置,应用程序右击无属性解决方法
- Struts2文件上传在配置fileUpload拦截器中设置文件大小无效的问题
- DIV的高度自动拉伸(height属性)在IE 7和Firefox下无效和解决方法
- 程序发布后报图表处理程序配置 [c:\TempImageFiles\] 中的临时目录无效的错误解决方法之一
- Struts2中配置全局拦截器的方法
- DIV的高度自动拉伸(height属性)在IE 7下无效和解决方法
- 无法识别的属性“type”+IIS没有Asp.net配置选项的解决方法(转)
- struts2的配置之could not find action or result 问题的解决方法(struts.xml文件路径)
- Struts2 OGNL表达式中调用java静态方法无效的解决办法
- html中span标签中width属性无效的解决方法
- 无法识别的属性“type”+IIS没有Asp.net配置选项的解决方法
- Windows2008 R2下,DCOM配置里的属性灰色不可用的解决方法