Struts2拦截器属性excludeMethods、includeMethods配置无效之解决方法
参考:http://blog.csdn.net/coolcoffee168/article/details/7963251
在配置struts2 拦截器属性excludeMethods、includeMethods进行方法过滤时发现不起作用。
经过查看书籍之后发现,要想使方法过滤配置起作用,拦截器需要继承MethodFilterInterceptor类。MethodFilterInterceptor类是AbstractInterceptor的子类,其源代码如下:
- 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
即可。
样例代码:
- 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配置无效之解决方法相关推荐
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
[SpringBoot]拦截器使用@Autowired注入接口为null解决方法 参考文章: (1)[SpringBoot]拦截器使用@Autowired注入接口为null解决方法 (2)https: ...
- linux配置网卡无效,Linux操作系统下网卡配置无效的解决方法
经常会出现这样的情况,用netconfig给网卡设置了一个ip地址例如192.168.1.2.然后在用 service network restart 的时候却给出错误的提示 :"eth0 ...
- Struts2 拦截器(Interceptor )原理和配置
Struts2 拦截器 拦截器在概念上与servlet过滤器或JDK代理类相同.拦截器允许横切功能,把action以及框架分开实现.你可以使用拦截器实现以下操作: 在调用action之前提供预处理逻辑 ...
- Struts2 拦截器 及如何获得 servlet 请求对象 以及Struts 基本配置 Session 超时设置...
在拦截器中可以三种实现 一:继承 AbstractInterceptor 类 二:继承 MethodFilterInterceptor类 三:实现 Interceptor 接口 在实现Intercep ...
- Struts2 拦截器的配置:
Struts2 拦截器的配置: 第一步:在struts.xml文件中加入拦截器配置: 我们可以在配置拦截器的时候,自己定义一个拦截器栈来包含我们自己定义的拦截器(注意:一定要将 defaultStac ...
- 【struts2】struts2拦截器
struts2提供面向切面(AOP)编程的机制,拦截器便是一种成熟的AOP编程思想的实现,它提供一种机制使开发者能把相对独立的代码抽象出来,配置到action前后执行.拦截器interceptor类似 ...
- Struts2拦截器的使用
如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈defaultStack,如下(这里我是引用了str ...
- 从struts2拦截器到自定义拦截器
http://www.cnblogs.com/withyou/p/3170440.html 拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.x ...
- Struts2拦截器的使用 (详解)
Struts2拦截器的使用 (详解) 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈default ...
最新文章
- android 学习随笔十二(网络:使用异步HttpClient框架)
- PowerDesigner12 逆向工程DataBase SQl2005: unable to list the tables 信息
- 毕加索发布轻量化转化引擎及BIMSOP协作云平台
- Jmeter之函数助手操作
- various,variable,varied,different, diverse的区别
- 怎么用计算机弹c哩c哩,计算器音乐c哩c哩乐谱 | 手游网游页游攻略大全
- 3-3HDFS架构详解
- 面向机器学习的特征工程 一、引言
- 数据库复习(前四章)
- mysql hive 安装 配置_hive 安装配置部署与测试
- JavaScript 从入门到放弃(二)模块化工具requirejs
- 定义返回函数指针(地址)的函数
- MySQL影响性能的因素
- SHELL(bash)脚本编程四:其他扩展
- 什么叫0day和Warez?
- win10系统迁移到固态硬盘ssd
- 真的明白Code、RO-data、RW-data和ZI-data吗?
- 三种T检验的详细区分
- 4K工业级高清4进1出DP自动USB KVM多电脑切换器(MT-PK401)
- svg文件解析(python)
热门文章
- TLD文件中body-content四种类型(能力工场)
- 中信国健临床通讯2011年7月期目录
- Java网络编程从入门到精通(1):Internet地址概述
- Kubernetes — 设计理念
- Linux 操作系统原理 — Namespace 资源隔离
- 逼格高又实用的 Linux 命令,运维同仁一定要懂
- 动漫人物VS编程语言
- 当我们在讨论奢侈品行业时,人工智能可以做什么?
- ERROR MESSAGE: Invalid command line: Malformed walker argument: Could not find walker with name
- PHP使用phpexcel读取excel文件