The first thing we should consider is that the workflow of figure 1.4 still obeys the sim- pler MVC view of the framework that we saw earlier. In the figure, the FilterDispatcher has already done its controller work by selecting the appropriate action to handle the request. The figure demonstrates what really happens when the action is invoked by the controller. As you can see, a few extra parts are added to the MVC basics. We’ll explain in the next paragraphs how the interceptors and the ActionContext aid the action and result in their processing of the request.

Figure 1.4 introduces the following new Struts 2 components: ActionContext, interceptors, the ValueStack, and OGNL. This diagram goes a long way toward showing what really happens in Struts 2. You could say that everything we’ll discuss in this book is shown in this diagram. As interceptors come first in the request-processing cycle, we’ll start with them. The name seems obvious, but what exactly do they intercept?

INTERCEPTORS

You may have noticed, while studying figure 1.4, that there is a stack of interceptors in front of the action. The invocation of the action must travel through this stack. This is a key part of the Struts 2 framework. We’ll devote an entire chapter to this important component later in the book. At this time, it is enough to understand that most every action will have a stack of interceptors associated with it. These interceptors are invoked both before and after the action, though we should note that they actually fire after the result has executed. Interceptors don’t necessarily have to do something both times they fire, but they do have the opportunity. Some interceptors only do work before the action has been executed, and others only do work afterward. The impor- tant thing is that the interceptor allows common, cross-cutting tasks to be defined in clean, reusable components that you can keep separate from your action code.

DEFINITION Interceptors are Struts 2 components that execute both before and after the rest of the request processing. They provide an architectural component in which to define various workflow and cross-cutting tasks so that they can be easily reused as well as separated from other archi- tectural concerns.

What kinds of work should be done in interceptors? Logging is a good example. Log- ging should be done with the invocation of every action, but it probably shouldn’t be put in the action itself. Why? Because it’s not part of the action’s own unit of work. It’s more administrative, overhead if you will. Earlier, we charged a framework with the responsibility of providing built-in functional solutions to common domain tasks such as data validation, type conversion, and file uploads. Struts 2 uses interceptors to do this type of work. While these tasks are important, they’re not specifically related to the action logic of the request. Struts 2 uses interceptors to both separate and reuse these cross-cutting concerns. Interceptors play a huge role in the Struts 2 framework. And while you probably won’t spend a large percentage of your time writing intercep- tors, most developers will find that many tasks are perfectly solved with custom inter- ceptors. As we said, we’ll devote all of chapter 4 to exploring this core component.

THE VALUESTACK AND OGNL

While interceptors may not absorb a lot of your daily development energies, the ValueStack and OGNL will be constantly on your mind. In a nutshell, the ValueStack is a storage area that holds all of the data associated with the processing of a request. You could think of it as a piece of scratch paper where the framework does its work while solving the problems of request processing. Rather than passing the data around, Struts 2 keeps it in a convenient, central location—the ValueStack.

OGNL is the tool that allows us to access the data we put in that central repository. More specifically, it is an expression language that allows you to reference and manip- ulate the data on the ValueStack. Developers new to Struts 2 probably ask more ques- tions about the ValueStack and OGNL than anything else. If you’re coming from Struts 1, you’ll find that these are a couple of the more exotic features of the new framework. Due to this, and the sheer importance of this duo, we’ll treat them care- fully throughout the book. In particular, chapters 5 and 6 describe the detailed func- tion of these two framework components.

DEFINITION Struts 2 uses the ValueStack as a storage area for all application domain data that will be needed during the processing of a request. Data is moved to the ValueStack in preparation for request processing, it is manipulated there during action execution, and it is read from there when the results render their response pages.

The tricky, and powerful, thing about the ValueStack and OGNL is that they don’t belong to any of the individual framework components. Looking back to figure 1.4, note that both interceptors and results can use OGNL to target values on the ValueStack. The data in the ValueStack follows the request processing through all phases; it slices through the whole length of the framework. It can do this because it is stored in a ThreadLocal context called the ActionContext.

DEFINITION OGNL is a powerful expression language (and more) that is used to refer- ence and manipulate properties on the ValueStack.

The ActionContext contains all of the data that makes up the context in which an action occurs. This includes the ValueStack but also includes stuff the framework itself will use internally, such as the request, session, and application maps from the Servlet API. You can access these objects yourself if you like; we’ll see how later in the book. For now, we just want to focus on the ActionContext as the ThreadLocal home of the ValueStack. The use of ThreadLocal makes the ActionContext, and thus the ValueStack, accessible from anywhere in the same thread of execution. Since Struts 2’s processing of each request occurs in a single thread, the ValueStack is available from any point in the framework’s handling of a request.

Typically, it is considered bad form to obtain the contents of the ActionContext yourself. The framework provides many elegant ways to interact with that data without actually touching the ActionContext, or the ValueStack, yourself. Primarily, you’ll use OGNL to do this. OGNL is used in many places in the framework to reference and

manipulate data in the ValueStack. For instance, you’ll use OGNL to bind HTML form fields to data objects on the ValueStack for data transfer, and you’ll use OGNL to pull data into the rendering of your JSPs and other result types. At this point, you just need to understand that the ValueStack is where your data is stored while you work with it, and that OGNL is the expression language that you, and the framework, use to target this data from various parts of the request-processing cycle.

2> How Struts 2 Works的更多相关文章

  1. 菜鸟学Struts2——Struts工作原理

    在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...

  2. spring注解源码分析--how does autowired works?

    1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...

  3. Struts的拦截器

    Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...

  4. Struts框架的核心业务

    Struts的核心业务 Struts核心业务有很多,这里主要介绍了比较简单一些的: 请求数据的处理,和数据自动封装,类型自动转换 1.Struts中数据处理 1.1.方式1:直接过去servletap ...

  5. Struts的文件上传下载

    Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...

  6. 配置hibernate,Struts。文件

    hibernate文件配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernat ...

  7. hibernate与Struts框架结合编写简单针对修改练习

    失败页面fail.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  8. 3. 解析 struts.xml 文件

    1. struts.xml 文件基本配置: 主要放在资源路径下,配置 sturts2相关的 Action , 拦截器等配置 <struts> <!-- 设置常量 --> < ...

  9. Struts+Spring+Hibernate项目的启动线程

    在Java Web项目中,经常要在项目开始运行时启动一个线程,每隔一定的时间就运行一定的代码,比如扫描数据库的变化等等.要实现这个功能,可以现在web.xml文件中定义一个Listener,然后在这个 ...

随机推荐

  1. 超越luabind的luaBridge

    此编是引用他人的文章,这里记录下,主要为以后自己查找方便,原文地址:http://www.cppblog.com/sunicdavy/archive/2013/12/07/204648.html 最近 ...

  2. IEEE 802.15.4协议学习之MAC层

        MAC负责建立于网络的同步,支持关联和取消关联.MAC层的安全以及控制物理信道访问机制.信道访问机制主要有以下几种:       1. 有序的物理无线信道访问机制     2. 协调器启动和维 ...

  3. 5.servlet cookie自动登录的实例

    1.要建的文档,.java用servlet创建 2.建一张登陆表格 index.jsp <%@ page language="java" import="java. ...

  4. VmodCam top verilog

    `timescale 1ns / 1ps /////////////////////////////////////////////////////////////////////////////// ...

  5. C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法

    下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumn CheckBox是否选中 在判断 ...

  6. Scrapy源码学习(一)

    用Scrapy已经有一段时间了,觉得该是看一下源码的时候了.最开始用的时候还是0.16的版本,现在稳定版已经到了0.18.结合使用Scrapy的过程,先从Scrapy的命令行看起. 一.准备 下载源代 ...

  7. 浅析php fwrite写入txt文件的时候用 \r\n不能换行的问题

    以下是对php中fwrite写入txt文件的时候用 \r\n不能换行的问题进行了介绍,需要的朋友可以过来参考下今天遇到了一个问题就是用fwrite写入txt文件的时候用 rn不能换行试了很久都没找到办 ...

  8. Messages.pas里的消息

    一.Windows 消息大全 这张表拷贝自万一兄的帖子:http://www.cnblogs.com/del/archive/2008/02/25/1079970.html 但是我希望自己能把这些消息 ...

  9. Oracle 动态视图6 V$PROCESS

    一.视图包含当前系统oracle运行的所有进程信息.常用于将session与进程(oracle进程,操作系统进程)之间建立联系. Column Datatype Description ADDR RA ...

  10. ros的源码阅读

    测试代码,使用xmlrpc与roscore通信 ros的框架是使用rpc与server端通信,server维护topic的publisher,subscriber,param server,servi ...