参考:http://blog.csdn.net/coolcoffee168/article/details/7963251

在配置struts2 拦截器属性excludeMethods、includeMethods进行方法过滤时发现不起作用。

经过查看书籍之后发现,要想使方法过滤配置起作用,拦截器需要继承MethodFilterInterceptor类。MethodFilterInterceptor类是AbstractInterceptor的子类,其源代码如下:

[java] view plaincopy
  1. public abstract class MethodFilterInterceptor extends AbstractInterceptor {
  2. protected transient Logger log = LoggerFactory.getLogger(getClass());
  3. protected Set<String> excludeMethods = Collections.emptySet();
  4. protected Set<String> includeMethods = Collections.emptySet();
  5. public void setExcludeMethods(String excludeMethods) {
  6. this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);
  7. }
  8. public Set<String> getExcludeMethodsSet() {
  9. return excludeMethods;
  10. }
  11. public void setIncludeMethods(String includeMethods) {
  12. this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods);
  13. }
  14. public Set<String> getIncludeMethodsSet() {
  15. return includeMethods;
  16. }
  17. @Override
  18. public String intercept(ActionInvocation invocation) throws Exception {
  19. if (applyInterceptor(invocation)) {
  20. return doIntercept(invocation);
  21. }
  22. return invocation.invoke();
  23. }
  24. protected boolean applyInterceptor(ActionInvocation invocation) {
  25. String method = invocation.getProxy().getMethod();
  26. // ValidationInterceptor
  27. boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method);
  28. if (log.isDebugEnabled()) {
  29. if (!applyMethod) {
  30. log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list.");
  31. }
  32. }
  33. return applyMethod;
  34. }
  35. /**
  36. * Subclasses must override to implement the interceptor logic.
  37. *
  38. * @param invocation the action invocation
  39. * @return the result of invocation
  40. * @throws Exception
  41. */
  42. protected abstract String doIntercept(ActionInvocation invocation) throws Exception;
  43. }

只需要实现该类中的

[java] view plaincopy
  1. protected abstract String doIntercept(ActionInvocation invocation) throws Exception

即可。

样例代码:

[java] view plaincopy
  1. package cua.survey.interceptor;
  2. import java.util.Map;
  3. import com.opensymphony.xwork2.Action;
  4. import com.opensymphony.xwork2.ActionContext;
  5. import com.opensymphony.xwork2.ActionInvocation;
  6. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
  7. import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
  8. public class LoginInterceptor extends MethodFilterInterceptor{
  9. private static final long serialVersionUID = 1L;
  10. protected String doIntercept(ActionInvocation action) throws Exception {
  11. Map<String, Object> session = ActionContext.getContext().getSession();
  12. String user = (String)session.get("user");
  13. if(user != null && !"".equals(user)){
  14. return action.invoke();
  15. }else{
  16. session.put("error", "your user or pwd is error, please login again...");
  17. return Action.LOGIN;
  18. }
  19. }
  20. }

实现之后 拦截器属性excludeMethods、includeMethods就可以起到作用了。

Struts2拦截器属性excludeMethods、includeMethods配置无效之解决方法相关推荐

  1. 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法

    [SpringBoot]拦截器使用@Autowired注入接口为null解决方法 参考文章: (1)[SpringBoot]拦截器使用@Autowired注入接口为null解决方法 (2)https: ...

  2. linux配置网卡无效,Linux操作系统下网卡配置无效的解决方法

    经常会出现这样的情况,用netconfig给网卡设置了一个ip地址例如192.168.1.2.然后在用 service network restart 的时候却给出错误的提示 :"eth0 ...

  3. Struts2 拦截器(Interceptor )原理和配置

    Struts2 拦截器 拦截器在概念上与servlet过滤器或JDK代理类相同.拦截器允许横切功能,把action以及框架分开实现.你可以使用拦截器实现以下操作: 在调用action之前提供预处理逻辑 ...

  4. Struts2 拦截器 及如何获得 servlet 请求对象 以及Struts 基本配置 Session 超时设置...

    在拦截器中可以三种实现 一:继承 AbstractInterceptor 类 二:继承 MethodFilterInterceptor类 三:实现 Interceptor 接口 在实现Intercep ...

  5. Struts2 拦截器的配置:

    Struts2 拦截器的配置: 第一步:在struts.xml文件中加入拦截器配置: 我们可以在配置拦截器的时候,自己定义一个拦截器栈来包含我们自己定义的拦截器(注意:一定要将 defaultStac ...

  6. 【struts2】struts2拦截器

    struts2提供面向切面(AOP)编程的机制,拦截器便是一种成熟的AOP编程思想的实现,它提供一种机制使开发者能把相对独立的代码抽象出来,配置到action前后执行.拦截器interceptor类似 ...

  7. Struts2拦截器的使用

    如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈defaultStack,如下(这里我是引用了str ...

  8. 从struts2拦截器到自定义拦截器

    http://www.cnblogs.com/withyou/p/3170440.html 拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.x ...

  9. Struts2拦截器的使用 (详解)

    Struts2拦截器的使用 (详解) 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈default ...

最新文章

  1. android 学习随笔十二(网络:使用异步HttpClient框架)
  2. PowerDesigner12 逆向工程DataBase SQl2005: unable to list the tables 信息
  3. 毕加索发布轻量化转化引擎及BIMSOP协作云平台
  4. Jmeter之函数助手操作
  5. various,variable,varied,different, diverse的区别
  6. 怎么用计算机弹c哩c哩,计算器音乐c哩c哩乐谱 | 手游网游页游攻略大全
  7. 3-3HDFS架构详解
  8. 面向机器学习的特征工程 一、引言
  9. 数据库复习(前四章)
  10. mysql hive 安装 配置_hive 安装配置部署与测试
  11. JavaScript 从入门到放弃(二)模块化工具requirejs
  12. 定义返回函数指针(地址)的函数
  13. MySQL影响性能的因素
  14. SHELL(bash)脚本编程四:其他扩展
  15. 什么叫0day和Warez?
  16. win10系统迁移到固态硬盘ssd
  17. 真的明白Code、RO-data、RW-data和ZI-data吗?
  18. 三种T检验的详细区分
  19. 4K工业级高清4进1出DP自动USB KVM多电脑切换器(MT-PK401)
  20. svg文件解析(python)

热门文章

  1. TLD文件中body-content四种类型(能力工场)
  2. 中信国健临床通讯2011年7月期目录
  3. Java网络编程从入门到精通(1):Internet地址概述
  4. Kubernetes — 设计理念
  5. Linux 操作系统原理 — Namespace 资源隔离
  6. 逼格高又实用的 Linux 命令,运维同仁一定要懂
  7. 动漫人物VS编程语言
  8. 当我们在讨论奢侈品行业时,人工智能可以做什么?
  9. ERROR MESSAGE: Invalid command line: Malformed walker argument: Could not find walker with name
  10. PHP使用phpexcel读取excel文件