Struts2 自定义拦截器(方法拦截器)
2013-09-27 11:24
465 查看
一、实现Interceptor接口
实现上述方法
二、继承AbstractInterceptor类,重写intercept()方法即可
此方法更可行,其实AbstractInterceptor类也就是实现了Interceptor接口
三、方法拦截器:继承MethodFilterInterceptor类,重写doIntercept()方法
MethodFilerInterceptor实现方法过滤中用到的两个参数
定义好自定义拦截器后,就要使用自定义拦截器,在struts.xml文档中
一、包内定义拦截器
二、action内使用拦截器
主要:可以看出使用了自定义拦截器的action要配置默认拦截器的引用,因为默认拦截器包含了参数的读取、session的管理等功能
一下是例子:
MyMethodInterceptor
struts.xml
1 | public interface Interceptor extends Serializable{ |
2 | public void init(); |
3 | public void destroy(); |
4 | public String intercept(ActionInvocation invocation)(); |
5 | } |
二、继承AbstractInterceptor类,重写intercept()方法即可
此方法更可行,其实AbstractInterceptor类也就是实现了Interceptor接口
1 | invocation.invoke();表示该方法执行完后执行Action的execute()方法或者执行下一个拦截器 |
2 | invocation.getAction(); 可以将该法强制转换为Action的类类型 |
MethodFilerInterceptor实现方法过滤中用到的两个参数
execludeMethods:该参数指定拦截器拒绝拦截的方法列表,多个方法用“,”隔开,指定了这个参数,拦截器不会拦截指定列表中的方法,就是所谓的黑名单 |
includeMethods:该参数指定拦截器需要拦截的方法列表,如果指定了参数,则指定的Action在执行前会被拦截,即白名单。 |
一、包内定义拦截器
1 | < package.... > |
2 | < interceptors > |
3 | < interceptor name = "myinterceptor" class = "...." > |
4 | </ interceptor > |
5 | </ interceptors > |
6 | </ package > |
1 | < action .....> |
2 | < result..... ></ result > |
3 | < interceptor-ref name = "defaultStack" ></ interceptor-ref > |
4 | < interceptor-ref name = "myinterceptor" ></ interceptor-ref > |
5 | </ action > |
一下是例子:
MyMethodInterceptor
01 | public class MyMethodInterceptor extends MethodFilterInterceptor{ |
02 | protected String doIntercept(ActionInvocation invocation) throws Exception { |
03 | // TODO Auto-generated method stub |
04 | System.out.println( "进入MyMethodInterceptor方法拦截器!!!!!!!!!!!!!" ); |
05 | Map session = invocation.getInvocationContext().getSession(); |
06 | String name = (String) session.get( "uname" ); |
07 | if (name != null ) { |
08 | return invocation.invoke(); |
09 | } |
10 | return "input" ; |
11 | } |
12 | } |
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" |
04 | "http://struts.apache.org/dtds/struts-2.3.dtd"> |
05 |
06 | < struts > |
07 | < constant name = "struts.i18n.encoding" value = "UTF-8" ></ constant > |
08 | < constant name = "struts.enable.DynamicMethodInvocation" value = "true" ></ constant > |
09 | <!-- <constant name="struts.action.extension" value="action,abc"></constant>--> |
10 |
11 | < package abstract = "true" name = "pk1" namespace = "/" extends = "struts-default" ></ package > |
12 |
13 | < package name = "pk2" extends = "pk1" > |
14 | < interceptors > |
15 | < interceptor name = "test1" class = "org.interceptors.MyTimerInterceptor" /> |
16 |
17 | < interceptor name = "method1" class = "org.interceptors.MyMethodInterceptor" > |
18 | < param name = "excludeMethods" >login</ param > |
19 | </ interceptor > |
20 |
21 | < interceptor-stack name = "myStack" > |
22 | < interceptor-ref name = "defaultStack" ></ interceptor-ref > |
23 | < interceptor-ref name = "method1" ></ interceptor-ref > |
24 | < interceptor-ref name = "test1" ></ interceptor-ref > |
25 |
26 | </ interceptor-stack > |
27 | </ interceptors > |
28 | <!--<default-interceptor-ref name="myStack"></default-interceptor-ref>--> |
29 |
30 | < action name = "login_*_*" class = "org.hzy.Actions.LoginAction" method = "{1}" > |
31 | < interceptor-ref name = "myStack" ></ interceptor-ref > |
32 | < result name = "success" type = "chain" >{2}</ result > |
33 | < result name = "input" >index.jsp</ result > |
34 | < result name = "error" >/WEB-INF/Jsp/error.jsp</ result > |
35 | </ action > |
36 |
37 | < action name = "query_*" class = "org.hzy.Actions.QueryAction" method = "{1}" > |
38 | < result >/WEB-INF/Jsp/show.jsp</ result > |
39 | </ action > |
40 |
41 | < action name = "upload_*" class = "org.hzy.Actions.FileUploadAction" method = "{1}" > |
42 | < result >/WEB-INF/Jsp/show.jsp</ result > |
43 | </ action > |
44 | </ package > |
45 | </ struts > |
相关文章推荐
- struts2拦截器-自定义拦截器,放行某些方法(web.xml配置)
- Struts2 自定义拦截器(方法拦截器)
- Struts2 自定义拦截器(方法拦截器)
- Struts2 自定义拦截器(方法拦截器)
- Struts2 自定义拦截器(方法拦截器)
- struts2自定义拦截器注解配置方法
- Struts2 自定义拦截器(方法拦截器)
- Struts2 自定义拦截器(方法拦截器)
- struts2自定义拦截器和方法拦截器
- 在struts2中配置自定义拦截器放行多个方法
- struts2中自定义拦截器intercept的相关配置及使用方法
- Struts2 自定义拦截器(方法拦截器)
- Struts2拦截器使用方法
- Struts2自定义拦截器
- Struts2自定义拦截器,实现发表文章检查是否已登陆功能
- 从struts2拦截器到自定义拦截器
- struts2自定义拦截器
- struts2方法拦截器
- 转载:Struts2拦截器之拦截器的方法过滤
- 29.struts2_自定义拦截器