使用拦截器的方法过滤特性
2015-05-10 16:59
288 查看
如果我们希望对Action中的部分方法实现拦截,Struts2为了实现方法过滤的功能,定义了一个名为MethodFilterInterceptor的类,该类继承了AbstractInterceptor。若想自定义的拦截器能够实现方法过滤的功能可以继承MethodFilterInterceptor类,该类重写了父类AbstractInterceptor的intercept方法,又提供了一个doIntercept的抽象方法,在此方法中用户可以自定义拦截器。
MethodFilterInterceptor提供了两个非常重要的方法,用于方法的过滤:
setIncludeMethods(String includeMethods)
setExcludeMethods(String excludeMethods),
setExcludeMethods的主要的作用是排除要过滤的方法,例如setExcludeMethods("execute"),这样Action的execute方法将不会被拦截,
setIncludeMethods方法正好相反,setIncludeMethods("execute"),这样execute方法将被拦截。
以硬编码的形式设置要过滤的方法的灵活性不够,因袭可以通过struts.xml配置文件来设置方法是否过滤是不错的选择:
<interceptor name="FilterhelloInterceptor">
<param name="excludeMethods">execute</param>
</interceptor>
struts.xml是通过属性来设置的,name="excludeMethods"表示要添加排除过滤的方法,当前配置的拦截过滤的方法时execute方法。若要排除多个方法,则使用都好分开
<interceptor name="FilterhelloInterceptor">
<param name="excludeMethods">execute,aa,bb,cc,dd</param>
</interceptor>
以上的配置过滤掉了execute,aa,bb,cc,dd方法,这些方法不会被拦截器所拦截。
也可以使用includeMethods参数指定要拦截的方法名。
<interceptor name="FilterhelloInterceptor">
<param name="includeMethods">execute,aa,bb,cc,dd</param>
</interceptor>
下面给出一段继承自MethodsFilterInterceptor类的自定义拦截器
package com.inter;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class FilterhelloInterceptor extends MethodFilterInterceptor {
private static final long serialVersionUID = 1L;
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("using the MethodFilterInterceptor...");
String result = invocation.invoke();
return result;
}
public void setExcludeMethods(String excludeMethods){
super.setExcludeMethods(excludeMethods);
}
@Override
public void setIncludeMethods(String includeMethods) {
super.setIncludeMethods(includeMethods);
}
}
MethodFilterInterceptor提供了两个非常重要的方法,用于方法的过滤:
setIncludeMethods(String includeMethods)
setExcludeMethods(String excludeMethods),
setExcludeMethods的主要的作用是排除要过滤的方法,例如setExcludeMethods("execute"),这样Action的execute方法将不会被拦截,
setIncludeMethods方法正好相反,setIncludeMethods("execute"),这样execute方法将被拦截。
以硬编码的形式设置要过滤的方法的灵活性不够,因袭可以通过struts.xml配置文件来设置方法是否过滤是不错的选择:
<interceptor name="FilterhelloInterceptor">
<param name="excludeMethods">execute</param>
</interceptor>
struts.xml是通过属性来设置的,name="excludeMethods"表示要添加排除过滤的方法,当前配置的拦截过滤的方法时execute方法。若要排除多个方法,则使用都好分开
<interceptor name="FilterhelloInterceptor">
<param name="excludeMethods">execute,aa,bb,cc,dd</param>
</interceptor>
以上的配置过滤掉了execute,aa,bb,cc,dd方法,这些方法不会被拦截器所拦截。
也可以使用includeMethods参数指定要拦截的方法名。
<interceptor name="FilterhelloInterceptor">
<param name="includeMethods">execute,aa,bb,cc,dd</param>
</interceptor>
下面给出一段继承自MethodsFilterInterceptor类的自定义拦截器
package com.inter;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class FilterhelloInterceptor extends MethodFilterInterceptor {
private static final long serialVersionUID = 1L;
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("using the MethodFilterInterceptor...");
String result = invocation.invoke();
return result;
}
public void setExcludeMethods(String excludeMethods){
super.setExcludeMethods(excludeMethods);
}
@Override
public void setIncludeMethods(String includeMethods) {
super.setIncludeMethods(includeMethods);
}
}
相关文章推荐
- 任何人都可以重构---使用 Eclipse 自动重构特性的方法与原因
- 任何人都可以重构---使用 Eclipse 自动重构特性的方法与原因 选择自 shanyou 的 Blog
- 关于在ehlib的DBGridEh控件中使用过滤功能的方法
- ehlib的DBGridEh控件中使用过滤功能的方法
- C# 3.0新特性初步研究 Part2:使用扩展方法
- 使用Spring内置拦截器为Struts动作在每个方法调用之前打印一句话
- 使用Hibernate过滤集合元素的两种方法
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- 改善C#程序的50种方法 条款4:使用Conditional特性代替#if条件编译
- 关于在ehlib的DBGridEh控件中使用过滤功能的方法
- 使用FilteredTextBoxExtender控件(过滤字符)的方法
- C#嵌套类的使用方法及特性
- 关于编码过滤的问题,这里使用在web.xml中添加过滤器的方法!
- 重写类中的equals方法进行过滤使用HashSet
- C#嵌套类的使用方法及特性
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- C#嵌套类的使用方法及特性 (转贴)
- 使用Spring内置拦截器为Struts的Action在每个方法调用之前打印一句话
- WebWork2.2新特性:使用国际化拦截器