溫故知新:struts2_06其他功能:攔截器
struts2的攔截器其實和過濾器是一個概念,雖然自定義的過濾器用的不多,但是struts2的核心功能都是用攔截器來實現的,異常捕獲,參數綁定,和國際化等等。
寫一個最簡單的過濾器的例子,感受一下自定義過濾器的使用就好。
先上action
寫一個最簡單的過濾器的例子,感受一下自定義過濾器的使用就好。
先上action
1 package demo.action;
2
3 public class InterceptorDemo {
4
5 private String name;
6
7 public String execute(){
8 System.out.println("我是Execute的方法");
9 System.out.println("name:" + name);
10 return "success";
11 }
12
13 //get/set方法略
14
15 }
16
創建一個interceptor只需要新建一個類,繼承struts2的AbstractInterceptor,重寫intercept方法2
3 public class InterceptorDemo {
4
5 private String name;
6
7 public String execute(){
8 System.out.println("我是Execute的方法");
9 System.out.println("name:" + name);
10 return "success";
11 }
12
13 //get/set方法略
14
15 }
16
1 package demo.interceptor;
2
3 import com.opensymphony.xwork2.ActionInvocation;
4 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
5
6 public class InterceptorDemo extends AbstractInterceptor{
7
8 private static final long serialVersionUID = -1739072578539674220L;
9
10 @Override
11 public String intercept(ActionInvocation invocation) throws Exception {
12
13 System.out.println("我是一個炫酷的攔截器");
14
15 //校驗用戶是否登陸等操作
16 //ServletActionContext.getContext().getSession()...
17
18 //校驗用戶是否有權限訪問等操作
19 //invocation.getProxy().getActionName()...
20
21 return invocation.invoke();
22 }
23
24 }
25
如此一來,攔截器就創建好了,接下來就是對攔截器的配置,看到struts.xml2
3 import com.opensymphony.xwork2.ActionInvocation;
4 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
5
6 public class InterceptorDemo extends AbstractInterceptor{
7
8 private static final long serialVersionUID = -1739072578539674220L;
9
10 @Override
11 public String intercept(ActionInvocation invocation) throws Exception {
12
13 System.out.println("我是一個炫酷的攔截器");
14
15 //校驗用戶是否登陸等操作
16 //ServletActionContext.getContext().getSession()...
17
18 //校驗用戶是否有權限訪問等操作
19 //invocation.getProxy().getActionName()...
20
21 return invocation.invoke();
22 }
23
24 }
25
1 <package name="interceptorDemo" extends="struts-default" namespace="/">
2
3 <!-- 需要在包內聲明攔截器,之后可以定義在action中定義對聲明攔截器的引用 -->
4 <interceptors>
5 <interceptor name="InterceptorDemo" class=demo.interceptor.InterceptorDemo"/>
6 </interceptors>
7
8 <!-- 在指定的action中使用指定的攔截器 -->
9 <action name="interceptorDemo"
10 class="demo.action.InterceptorDemo"
11 method="execute">
12 <interceptor-ref name="InterceptorDemo"/>
13 </action>
將定義好的攔截器添加到某個action的定義中,讓攔截器攔截對應的action,但如果如上述那樣去配置的話,除了自定義攔截器的功能,struts2 的攔截器功能都會失效,action中的name值根本拿不到。配置中的每個包都會繼承struts-default這個包,簡單看一下struts-default.xml這個文件2
3 <!-- 需要在包內聲明攔截器,之后可以定義在action中定義對聲明攔截器的引用 -->
4 <interceptors>
5 <interceptor name="InterceptorDemo" class=demo.interceptor.InterceptorDemo"/>
6 </interceptors>
7
8 <!-- 在指定的action中使用指定的攔截器 -->
9 <action name="interceptorDemo"
10 class="demo.action.InterceptorDemo"
11 method="execute">
12 <interceptor-ref name="InterceptorDemo"/>
13 </action>
14
15 </package> 1 <interceptor-stack name="defaultStack">
2 <interceptor-ref name="exception"/>
3 <interceptor-ref name="alias"/>
4 <interceptor-ref name="servletConfig"/>
5 <interceptor-ref name="i18n"/>
6 <interceptor-ref name="prepare"/>
7 <interceptor-ref name="chain"/>
8 <interceptor-ref name="scopedModelDriven"/>
9 <interceptor-ref name="modelDriven"/>
10 <interceptor-ref name="fileUpload"/>
11 <interceptor-ref name="checkbox"/>
12 <interceptor-ref name="multiselect"/>
...
<interceptor-ref name="debugging"/>
</interceptor-stack>
上述就是部分配置,定義了默認的攔截器棧,也就是一組攔截器,從名字就可以看出很多功能都由攔截器完成,所以,自定義的攔截器也需要引用這一組攔截器,需要在配置中添加默認攔截器棧2 <interceptor-ref name="exception"/>
3 <interceptor-ref name="alias"/>
4 <interceptor-ref name="servletConfig"/>
5 <interceptor-ref name="i18n"/>
6 <interceptor-ref name="prepare"/>
7 <interceptor-ref name="chain"/>
8 <interceptor-ref name="scopedModelDriven"/>
9 <interceptor-ref name="modelDriven"/>
10 <interceptor-ref name="fileUpload"/>
11 <interceptor-ref name="checkbox"/>
12 <interceptor-ref name="multiselect"/>
...
<interceptor-ref name="debugging"/>
</interceptor-stack>
...
<default-interceptor-ref name="default-stack"/>
...
1 <action name="interceptorDemo"
2 class="org.duyt.action.InterceptorDemo"
3 method="execute">
4 <interceptor-ref name="InterceptorDemo"/>
5 <interceptor-ref name="defaultStack"/>
6 </action>
或者定義一組攔截器棧2 class="org.duyt.action.InterceptorDemo"
3 method="execute">
4 <interceptor-ref name="InterceptorDemo"/>
5 <interceptor-ref name="defaultStack"/>
6 </action>
1 <interceptors>
2 <interceptor name="InterceptorDemo" class="demo.interceptor.InterceptorDemo"/>
3
4 <!-- 聲明的攔截器棧一定要引用defaultStack,不然struts2無法發揮作用,參數封裝等最基本的功能都會失效 -->
5 <interceptor-stack name="selfStack">
6 <interceptor-ref name="InterceptorDemo"/>
7 <interceptor-ref name="defaultStack"/>
8 </interceptor-stack>
9 </interceptors>
10
11 <!-- 在指定的action中使用指定的攔截器 -->
12 <action name="interceptorDemo"
13 class="demo.action.InterceptorDemo"
14 method="execute">
15 <interceptor-ref name="selfStack"/>
16 </action>
這樣,自定義的攔截器就能融入到完整的功能之中。2 <interceptor name="InterceptorDemo" class="demo.interceptor.InterceptorDemo"/>
3
4 <!-- 聲明的攔截器棧一定要引用defaultStack,不然struts2無法發揮作用,參數封裝等最基本的功能都會失效 -->
5 <interceptor-stack name="selfStack">
6 <interceptor-ref name="InterceptorDemo"/>
7 <interceptor-ref name="defaultStack"/>
8 </interceptor-stack>
9 </interceptors>
10
11 <!-- 在指定的action中使用指定的攔截器 -->
12 <action name="interceptorDemo"
13 class="demo.action.InterceptorDemo"
14 method="execute">
15 <interceptor-ref name="selfStack"/>
16 </action>
posted on 2014-11-02 09:28 都較瘦 閱讀(145) 評論(0) 編輯 收藏 所屬分類: MVCFramework