读书人

转: Struts 2的拦截器(8)下

发布时间: 2012-08-30 09:55:54 作者: rapoo

转: Struts 2的拦截器(8)上

拦截器(Interceptor)是Struts 2的核心组成部分。很多功能(Feature)都是构建在拦截器基础之上的,例如文件的上传和下载、国际化、转换器和数据校验等,Struts 2利用内建的拦截器,完成了框架内的大部分操作。

在Struts 2文档中对拦截器的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制,使开发者可以定义一个特定的功能模块,这个模块可以在Action执行之前或者之后运行,也可以在一个Action执行之前阻止Action执行。同时也提供了一种可以提取Action中可重用的部分的方式。

5.2??Struts 2拦截器

通过上一节的介绍,读者会对拦截器的原理有了一个基本的理解,本节来介绍Struts 2架构中的拦截器。

5.2.1??Struts 2拦截器原理

读者可以通过Struts 2官方文档说明来理解拦截器,如果读者具有较强的英文阅读能力,建议查阅官方使用文档(http://struts.apache.org/2.0.11/docs/interceptors.html)。如图5.4所示,是拦截器在Struts 2中的示意图。

转: Struts 2的拦截器(8)下

图5.4??Struts 2拦截器

从图5.4所示内容中可以看出,Struts 2架构的Action被一个或者多个拦截器(拦截器栈)所包围,所有的用户请求都会被拦截器所拦截,然后交给Action处理,处理结果以逻辑视图方式返回给用户。而这个调用执行流程,是由Struts 2的配置文件来实现的,后面会详细介绍。拦截器是Struts 2核心部分之一。

当用户请求到达Struts 2的ServletDispatcher时,Struts 2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表(List),最后一个一个地调用列表中的拦截器。拦截器时序图如图5.5所示。

转: Struts 2的拦截器(8)下

图5.5??Struts 2拦截器时序图

在Struts 2架构中,Action的调用都是通过拦截器来实现的。有的读者可能会疑惑,前几章的介绍中,没有明确说明拦截器,为什么可以直接调用Action?那是因为Struts 2架构如果不做显式的拦截器配置,则系统会调用默认的拦截器来调用Action,在用户看来,好像没有配置拦截器。系统默认的拦截器笔者后面会详细介绍。

5.2.2??HelloWorld拦截器

读者可以按照笔者的步骤,一步一步建立第一个拦截器示例,来体验Struts 2框架中拦截器带给读者的惊喜。

(1)?在配置文件中增加拦截器定义和在Action中声明拦截器。笔者建立了一个目录ch5来对应本章,ch5.xml配置文件内容如代码5.8所示。

代码5.8??HelloWorld拦截器配置文件

?

转: Struts 2的拦截器(8)下<?xml?version="1.0"?encoding="UTF-8"??>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下<!DOCTYPE?struts?PUBLIC
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????"http://struts.apache.org/dtds/struts-2.0.dtd">
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下<struts>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????<!--??定义包-->
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下<package?name="ch5"?extends="struts-default"?namespace="/ch5">
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下??????<!--??定义拦截器-->
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????<interceptors>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????????<interceptor?name="Myinterceptor"
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????????????class="ch5.Myintercepor">
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????????</interceptor>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????</interceptors>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????<action?name="Reg"?class="ch5.Reg">
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????????<result?name="success">/ch5/success.jsp</result>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????????<result?name="input">/ch5/reg.jsp</result>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????????<!--??引用默认拦截器-->
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????????<interceptor-ref?name="defaultStack"></interceptor-ref>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????????<!--??引用自定义默认拦截器-->
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????????<interceptor-ref?name="Myinterceptor"></interceptor-ref>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????</action>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????</package>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下</struts>
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下

?

★?注意?★

需要读者在struts.xml文件中使用<include file="/ch5/ch5.xml"></include>将该配置文件包含进去。

?

?(2)在ch5包内建立一个拦截器类Myintercepor,如代码5.9所示。

代码5.9??Helloworld拦截器类Myintercepor

?

转: Struts 2的拦截器(8)下package?ch5;
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下import?com.opensymphony.xwork2.ActionInvocation;
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下import?com.opensymphony.xwork2.interceptor.AbstractInterceptor;
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下public?class?Myintercepor?extends?AbstractInterceptor?{
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????//拦截方法
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????public?String?intercept(ActionInvocation?arg0)?throws?Exception?{
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????Reg?reg?=?(Reg)?arg0.getAction();
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????System.out.println("拦截器信息:HelloWorld拦截器!");
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????//执行Action或者执行下一个拦截器
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????String?result?=?arg0.invoke();
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????//提示Action执行完毕
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????System.out.println("拦截器信息:Action执行完毕!");
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????????return?result;
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下????}
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下}
转: Struts 2的拦截器(8)下
转: Struts 2的拦截器(8)下

?

?

读书人网 >软件架构设计

热点推荐