拦截器(Interceptor)是 Struts 2 的核心组成部分。

  1. Struts2 很多功能都是构建在拦截器基础之上的,例如文件的上传和下载、国际化、数据类型转换和数据校验等等。
  2. Struts2 拦截器在访问某个 Action 方法之前或之后实施拦截 Struts2 拦截器是可插拔的, 拦截器是 AOP(面向切面编程) 的一种实现.
  3. 拦截器栈(Interceptor Stack): 将拦截器按一定的顺序联结成一条链. 在访问被拦截的方法时,
  4. Struts2 拦截器链中的拦截器就会按其之前定义的顺序被依次调用

Struts2拦截器再认识_拦截器

下面说明了栈的调用次序:

1 <package name="default" extends="struts-default">
2 <!--配置拦截器-->
3 <interceptors>
4 <interceptor name="FirstInterceptor" class="Interceptor.FirstInterceptor">
5 <param name="value" >YEN</param>
6 </interceptor>
7 <interceptor name="SecondInterceptor" class="Interceptor.SecondInterceptor"></interceptor>
8         //次序为 FirstInterceptor==》SecondInterceptor===》defaultStack===》SecondInterceptor====》FirstInterceptor
9 <interceptor-stack name="AllInterceptor">
10 <interceptor-ref name="FirstInterceptor"></interceptor-ref>
11 <interceptor-ref name="SecondInterceptor"></interceptor-ref>
12 <interceptor-ref name="defaultStack"></interceptor-ref>
13 </interceptor-stack>
14 </interceptors>
15
16 <action name="LoginAction" class="Action.LoginAction">
17 <interceptor-ref name="AllInterceptor"></interceptor-ref>
18 <result name="success">success.jsp</result>
19 </action>

 

Struts2拦截器再认识_拦截器_02

struts2自带的拦截器:

Struts2拦截器再认识_拦截器_03

Struts2拦截器再认识_xml_04

 

 

自定义规则

struts2提供的 interceptor-stack 不是让我们偷懒的,很多时候,我定义自己很多的拦截器,每一个拦截器在不同业务场景下是不同的,我们不能为了图方便,把所以拦截器全部配置为一个拦截器栈来使用,这回极大的加强服务器的负担,

比如说:methodFirstceptor 是一个方法拦截器,我们配置需要拦截的方法,而function 拦截器又不需要拦截相关的方法,所以就没有必要把他们放在一起。

<!-- 拦截器栈配置属于自己的拦截器规则 -->
<interceptor-stack name="mystack">
<interceptor-ref name="methodFirstceptor"></interceptor-ref>
<interceptor-ref name="funtion"></interceptor-ref>
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">2097152</param>
<!-- <param name="fileUpload.allowedTypes">text/html,text/xml</param> -->
<param name="fileUpload.allowedExtensions">html,dtd,xml</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>

 注意:默认拦截器 也叫全局拦截器 (也就是说我们 一定要注意在配置 默认拦截器的时候 不要加过多无用的拦截器,只要那些都需要作用到每个Action上的拦截器,才能配置拦截器栈 以及默认拦截器)

1   <!-- 为此包下的所有action应用拦截器 -->  
2 <default-interceptor-ref name="permissionStack" />

普通拦截器:

1 //可以实现Interceptor
2 public class MyInterceptor extends AbstractInterceptor {
3
4 /**
5 *
6 */
7 private static final long serialVersionUID = 1L;
8 @Override
9 public String intercept(ActionInvocation invocation) throws Exception {
10
11 System.out.println("before invocation.invoke...");
12
13 //可以不调用
14 String result = invocation.invoke();
15
16 System.out.println("after invocation.invoke...");
17
18 return "success";
19 }
20

方法拦截器:

1 public class MethodFirstceptor extends MethodFilterInterceptor {
2 /**
3 *
4 */
5 private static final long serialVersionUID = 1L;
6
7 @Override
8 protected String doIntercept(ActionInvocation aInvocation) throws Exception {
9 System.out.println("进入方法拦截器");
10 String str = aInvocation.invoke();
11 System.out.println("退出方法拦截器");
12 return str;
13 }
14
15 @Override
16 public Set<String> getExcludeMethodsSet() {
17 // TODO Auto-generated method stub
18 return super.getExcludeMethodsSet();
19 }
20
21 @Override
22 public Set<String> getIncludeMethodsSet() {
23 // TODO Auto-generated method stub
24 return super.getIncludeMethodsSet();
25 }
26

 

struts2配置文件:

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4 "http://struts.apache.org/dtds/struts-2.3.dtd">
5
6 <struts>
7
8
9 <!-- 配置国际化资源文件 -->
10 <constant name="struts.custom.i18n.resources" value="i18n"></constant>
11 <constant name="struts.devMode" value="true"></constant>
12
13 <package name="default" namespace="/" extends="struts-default">
14
15 <interceptors>
16 <!--方法拦截器配置 -->
17 <interceptor name="methodFirstceptor"
18 class="interceptors.MethodFirstceptor">
19 <!--配置参数 :拦截什么方法 -->
20 <!--includeMethods控制能访问哪些 -->
21 <param name="includeMethods">add</param>
22 <!--excludeMethods控制能访问哪些 -->
23 </interceptor>
24
25 <!-- 自定义拦截器 -->
26 <interceptor name="hello"
27 class="interceptors.MyInterceptor"></interceptor>
28
29 <!-- 拦截器栈配置属于自己的拦截器规则 -->
30 <interceptor-stack name="mystack">
31 <interceptor-ref name="methodFirstceptor"></interceptor-ref>
32 <interceptor-ref name="hello"></interceptor-ref>
33 <interceptor-ref name="defaultStack">
34 <param name="fileUpload.maximumSize">2097152</param>
35 <!-- <param name="fileUpload.allowedTypes">text/html,text/xml</param> -->
36 <param name="fileUpload.allowedExtensions">html,dtd,xml</param>
37 </interceptor-ref>
38 <interceptor-ref name="defaultStack"></interceptor-ref>
39 </interceptor-stack>
40
41 </interceptors>
42
43 <default-interceptor-ref name="mystack"></default-interceptor-ref>
44
45 <!-- 执行默认的方法的时候 是不会被拦截的 -->
46 <action name="loginAction" class="action.LoginAction">
47 <interceptor-ref name="mystack"></interceptor-ref>
48 <result>/success.jsp</result>
49 </action>
50
51 <!-- 执行要拦截的方法 -->
52 <action name="addAction" class="action.LoginAction" method="add">
53 <interceptor-ref name="mystack"></interceptor-ref>
54 <result>/success.jsp</result>
55 </action>
56
57
58
59
60 <action name="testUpload" class="upload.UploadAction">
61 <result>/success.jsp</result>
62 <result name="input">/upload.jsp</result>
63 </action>
64
65 <action name="testUpload2" class="upload.MulUploadAction">
66 <result>/success.jsp</result>
67 <result name="input">/uploadmul.jsp</result>
68 </action>
69
70 <action name="testDownload" class="download.DownLoadAction">
71 <result type="stream">
72 <param name="bufferSize">2048</param>
73 <!-- <param name="contentType">${contentType}</param> -->
74 <!-- 调用当前action中的getContentType()方法 -->
75 <param name="contentDisposition">attachment;filename=${filename}</param>
76 <param name="inputStream">${inputStream}</param>
77 <!-- 调用当前action中的getInputStream()方法 -->
78 </result>
79 </action>
80
81 <!-- 表单重复提交问题 -->
82
83 <action name="testToken" class="token.TokenAction">
84 <interceptor-ref name="mystack"></interceptor-ref>
85 <!-- 会转到标记为invalid-token的页面 -->
86 <!-- <interceptor-ref name="token"></interceptor-ref> -->
87 <!-- 停留在当前页面,不做任何操作 -->
88 <interceptor-ref name="tokenSession"></interceptor-ref>
89 <!-- 必须调用默认的拦截器 -->
90 <interceptor-ref name="defaultStack"></interceptor-ref>
91 <result>/success.jsp</result>
92 <result name="invalid.token">/token-error.jsp</result>
93 </action>
94
95 </package>
96
97
98 </struts>

 

<package name="default" extends="struts-default">
<!--配置拦截器--><interceptors>
<interceptor name="FirstInterceptor" class="Interceptor.FirstInterceptor">
<param name="value" >YEN</param>
</interceptor>
<interceptor name="SecondInterceptor" class="Interceptor.SecondInterceptor">
</interceptor>
<interceptor-stack name="AllInterceptor">
<interceptor-ref name="FirstInterceptor">
</interceptor-ref>
<interceptor-ref name="SecondInterceptor">
</interceptor-ref>
<interceptor-ref name="defaultStack">
</interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="LoginAction" class="Action.LoginAction">
<interceptor-ref name="AllInterceptor">
</interceptor-ref>
<result name="success">success.jsp</result>
</action>