5、Struts2自定义拦截器

一、拦截器相关知识

1、Struts2框架剖析

Holly版本生活案例:

影视公司(拍电影)    ActionMapper

传媒公司(包装明星) ActionMapping

明星                        Action

经纪人                     ActionProxy(代理对象)

小工所在单位             ActionInvocation

小工                        Interceptor(拦截器)

递归==99归一

2、struts2工作原理

3、拦截器工作原理

拦截器围绕着Action和Result的执行而执行。拦截器的工作原理类似递归的九九归一!

拦截器执行的三个阶段,有条件的执行周期:

(1)做一些Action执行前的预处理:拦截器可以准备、过滤、改变或者操作任何可以访问的数据,包括Action。

(2)调用ActionInvocation的invoke()方法将控制交给后续拦截器或返回结果字符串终止执行:如果拦截器决定请求的处理不应该继续,可以不调用invoke()方法,而是直接返回一个控制字符串。通估这种方式,可以停止后续的执行,并且决定哪个结果呈现给客户端。

(3)做一些Action执行后的处理:此时拦截器依然可以改变可以访问的对象和数据,只是此时框架已经选择了一个结果呈现给客户端了。

 

二、自定义拦截器

1、创建如下项目结构

2、在src下的com.action包下创建MyTimerAction.java

 1 package com.action;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 
 5 /**
 6  * 记录执行时间的Action
 7  * @author Holly老师
 8  *
 9  */
10 public class MyTimerAction implements Action {
11 
12     public String execute() throws Exception {
13         System.out.println("Action记录执行时间");
14         return SUCCESS;
15     }
16 
17 }
MyTimerAction.java

3、在src下的com.interceptor包下创建自定义拦截器类MyTimerInterceptor.java

 1 package com.interceptor;
 2 
 3 import org.apache.struts2.ServletActionContext;
 4 
 5 import com.opensymphony.xwork2.ActionInvocation;
 6 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 7 import com.opensymphony.xwork2.interceptor.Interceptor;
 8 /**
 9  * 自定义拦截器:
10  * 记录动作执行所花费的时间
11  * @author pc
12  *
13  */
14 public class MyTimerInterceptor implements Interceptor{
15 
16     public void destroy() {
17         System.out.println("销毁的方法....");
18         
19     }
20 
21     public void init() {
22     System.out.println("初始化方法....");
23         
24     }
25 
26     
27     /**
28      * 拦截器执行的入门方法(小工的某个技能)
29      * @param  invocation 经纪人
30      */
31     public String intercept(ActionInvocation invocation) throws Exception {
32         //1.执行Action之前的工作:获取开始执行的时间
33         long startTime=System.currentTimeMillis();
34         System.out.println("执行Action之前的工作,开始时间"+startTime);
35         
36         //2.执行后续的拦截器或Action(接收经纪人的口令)
37         String result=invocation.invoke();
38         
39         //3.执行Action之后的工作:计算并输出执行的时间
40         long endTime=System.currentTimeMillis();
41         
42         long execTime=endTime-startTime;
43         
44         System.out.println("执行Action后的,结束时间"+endTime);
45         System.out.println("总共用时:"+execTime);
46         
47         //返回结果字符串 (经纪人口令给下一个小工)
48         return result;
49         
50     }
51     
52 
53 }
MyTimerInterceptor .java

4、在src下创建struts.xml文件并配置拦截器

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" >
 3 <struts>
 4      <!-- 中文乱码处理 -->
 5      <constant name="struts.i18n.encoding" value="UTF-8"/>
 6       
 7     <package name="default" namespace="/" extends="struts-default">
 8     <!-- 配置所有拦截器的节点 -->
 9      <interceptors>
10        <interceptor name="myTimer" class="com.interceptor.MyTimerInterceptor"></interceptor>
11      </interceptors> 
12      
13      <!-- 配置Action=明星 -->
14       <action name="myTimer" class="com.action.MyTimerAction">
15         <result>/index.jsp</result>
16         <!-- 引用拦截器==小工 -->
17         <interceptor-ref name="myTimer"></interceptor-ref>
18         <interceptor-ref name="defaultStack"></interceptor-ref>
19       </action>  
20     </package>
21 </struts>
struts.xml

5、编辑WebRoot下的WEB-INF下的web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 3     <filter>
 4         <filter-name>struts2</filter-name>
 5         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 6     </filter>
 7 
 8     <filter-mapping>
 9         <filter-name>struts2</filter-name>
10         <url-pattern>/*</url-pattern>
11     </filter-mapping>
12 
13     <welcome-file-list>
14         <welcome-file>index.jsp</welcome-file>
15     </welcome-file-list>
16 
17 </web-app>
web.xml

6、运行项目

7、查看控制台的输出

 

posted @ 2016-05-23 23:02  红酒人生  阅读(606)  评论(0编辑  收藏  举报