Struts2拦截指定方法的拦截器
作者:禅楼望月
默认情况下,我们为一个Action配置一个拦截器,该拦截器会拦截该Action中的所有方法,但是有时候我们只想拦截指定的方法。为此,需要使用struts2拦截器的方法过滤特性。
要使用struts2拦截器的方法过滤特性其实也很简单,只需让拦截器的实现类继承com.opensymphony.xwork2.interceptor.MethodFilterInterceptor类。该类是AbstractInterceptor的子类。它重写了AbstractInterceptor类的intercept(ActionInvocation invocation)方法,并提供了protected abstract String doIntercept(ActionInvocation invocation) throws Exception方法。拦截器的实现类只需重写该方法即可。
import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; import com.stbc.web.Action.LoginAction; public class InterceptorHello extends MethodFilterInterceptor { private static final long serialVersionUID = -5407269431454126006L; private String name; public void setName(String name) { this.name = name; } @Override protected String doIntercept(ActionInvocation invocation) throws Exception { HttpServletRequest request=ServletActionContext.getRequest(); if(request.getParameter("username").equals("毛爷爷")){ return "error"; }else { request.setAttribute("datetime", new Date()); LoginAction action=(LoginAction) invocation.getAction(); return action.execute(); } } }
从上面的代码可以看出,我们自定义的拦截指定方法的拦截器和普通的拦截器没有太大的区别,只不过是继承的类和重写的方法不同而已。
那么我们怎么来实现一些方法不拦截而另一些方法拦截呢?
查看MethodFilterInterceptor类的API可知:
在Action中配置也很简单,它也是通过反射的技术来实现的。
<package name="loginPackage" extends="struts-default" namespace="/login"> <interceptors> <interceptor name="interceptormaoyeye" class="test.InterceptorHello"> <param name="name">你好!</param> </interceptor> </interceptors> <action name="login" class="com.stbc.web.Action.LoginAction"> <result name="success">welcome.jsp</result> <result name="error">welcome.jsp</result> <interceptor-ref name="interceptormaoyeye"> <!-- 这里使用了反射的方法,实际上调用的是拦截器类的setName方法 --> <param name="name">覆盖了默认值</param> <!-- 有多个方法需要(不)被拦截器拦截,则多个方法名之间用逗号分隔即可。 --> <param name="excludeMethods">method1,method2</param> <param name="includeMethods">method2,method3</param> </interceptor-ref> </action> </package>
上述代码将method2配置为既不给拦截也被拦截,这是struts2以<param name="includeMethods">的为准。
struts2内置的支持方法过滤的拦截器有:
欢迎转载,请注明出处。
Struts2拦截指定方法的拦截器的更多相关文章
- SpringBoot —— AOP注解式拦截与方法规则拦截
AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. SpringBoot中AOP的使用 ...
- struts2中的方法过滤拦截器
方法过滤拦截器是只过滤指定的方法,如果使用针对action 的普通的过滤器则会过滤该action内部 所有方法.如果在一个action中同时有多个作为业务逻辑控制的方法存在 的话则会过滤所有的业务逻辑 ...
- Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...
- SpringBoot AOP注解式拦截与方法规则拦截
AOP的本质还是动态代理对方法调用进行增强. SpringBoot 提供了方便的注解实现自定义切面Aspect. 1.使用需要了解的几个概念: 切面.@Aspect 切点.@Pointcut. 通知. ...
- struts2复习(五)拦截器总结
1. 拦截器(Interceptor): 拦截器是Struts2的核心,Struts2的众多功能都是通过拦截器来实现的. 2. 拦截器的配置 1)编写实现Interceptor 接口的类. 2)在s ...
- 【Spring开发】—— AOP之方法级拦截
前言: 前面介绍了Spring的核心模块以及相关的依赖注入等概念.这篇讲解一下spring的另一个重点,AOP面向切面编程. 说道AOP不得不提到几个概念: 切面:也就是我们自己的一些业务方法. 通知 ...
- AOP通知无法切入指定方法
AOP通知,切入指定方法时拦截不到,可能是拦截的方法本身是被本类的其他方法调用的,根据AOP反射原理是无法拦截本类中方法调用的方法的.如: class AImpl implements AIf { s ...
- struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证
struts2.xml 内容 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...
- struts2 javaweb 过滤器、监听器 拦截器 原理
转: 过滤器.监听器 拦截器 过滤器 创建一个 Filter 只需两个步骤: (1)创建 Filter 处理类: (2)在 web.xml 文件中配置 Filter . 创建 Filter 必须实现 ...
随机推荐
- ABAP术语-Call Transaction
Call Transaction 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/15/1039270.html A data transfe ...
- keepalived入门
简介 Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服 ...
- 【控制连接实现信息共享---linux和设备下ssh和远程连接telnet服务的简单搭建】
SSH的配置 空密码登陆ssh server 如果要登录ssh server通常要在server和client之间采取具有共同加密的秘钥,若每次当client想要了:连接ssh server时都要手工 ...
- php xml转数组 自定义xml_to_array
<?php header("Content-type: text/xml; charset=utf-8"); $con = file_get_contents('xml路径' ...
- php+sqlserver处理读取decimal 类型数据,不满1的数字会去掉0的问题
php+sqlserver处理读取decimal 类型数据,如果数据不满1,会去掉0的问题.比如读取的数据是 0.05,会显示 .05 function convert_number($number) ...
- 20190105-打印字母C,H,N,口等图像和杨辉三角
1. 打印字母C ****** * * * * ****** def print_c(n): print('*' * n) for i in range(n): print('* ') print(' ...
- zabbix配置报警媒介-用户-动作-邮件脚本触发mailx邮件报警
2018-09-16更新,新版本zabbix不需要使用脚本发送邮件,在zabbix web界面直接配置就可以 配置邮件参数,测试发送邮件 确认安装相关服务,centos7默认安装 [root@VM_1 ...
- python 面向对象 (多态)
什么是多态?多态就像是人有多种心情,场景不一样心情就会不一样. class Dog: def print_self(self): print('this is dog') class Hsq(Dog) ...
- python基础之IO模型
IO模型分类 五种IO Model blocking IO 阻塞IO nonblocking IO 非阻塞IO IO multiplexing IO多路复用 signal driven IO 信号驱动 ...
- c/c++ 结构体传参问题
c/c++的结构体传参可以有三种方式: 1.传递结构体变量,值传递 2.传递结构体指针,地址传递 3.传递结构体成员,可是值传递也可以是地址传递 根据代码示例: 1.传递结构体变量 #include& ...