Figure 1.1. The Java Servlet API exposes the HTTP client/server protocol to the Java platform. Struts 2 is built on top of that.
|
For web applications, HTTP has two hurdles to get over. It’s stateless, and it’s text based.
Don’t reinvent the wheel
FilterDispatcher: early version
Figure 1.4. Struts 2 request processing uses interceptors that fire before and after the action and result.
the ValueStack is a storage area that holds all of the data associated with the processing of a request.
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.
OGNL is a powerful expression language (and more) that is used to reference and manipulate properties on the ValueStack.
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.
Figure 2.6. Anatomy of a URL: mapping a URL namespace to a Struts 2 action namespace
Actions do three things. First, as you probably understand by now, an action’s most important role, from the perspective of the framework’s architecture, is encapsulating the actual work to be done for a given request. The second major role is to serve as a data carrier in the framework’s automatic transfer of data from the request to the view. Finally, the action must assist the framework in determining which result should render the view that’ll be returned in the request response.
This important params interceptor has been the one moving data from the request parameters to our action’s JavaBeans properties.
the DefaultWorkflowInterceptor, to provide basic validation.
FileUploadInterceptor.
Figure 4.1. ActionInvocation encapsulates the execution of an action with its associated interceptors and results. |
We need to point out one important detail before moving on. Interceptor instances are shared among actions. Though a new instance of an action is created for each request, interceptors are reused. This has one important implication. Interceptors are stateless. Don’t try to store data related to the request being processed on the interceptor object. This isn’t the role of the interceptor. An interceptor should just apply its processing logic to the data of the request, which is already conveniently stored in the various objects you can access through the ActionInvocation.
OGNL is a powerful technology that’s been integrated into the Struts 2 framework to help with data transfer and type conversion. OGNL is the glue between the framework’s string-based HTTP input and output and the Java-based internal processing.
Figure 5.1. OGNL provides the framework’s mechanism for transferring and type-converting data.
|
The ValueStack is a Struts 2 construct that presents an aggregation of the properties of a stack of objects as properties of a single virtual object. If duplicate properties exist—two objects in the stack both have a name property—then the property of the highest object in the stack will be the one exposed on the virtual object represented by the ValueStack. The ValueStack represents the data model exposed to the current request and is the default object against which all OGNL expressions are resolved.
The Struts 2 framework comes with built-in support for converting between the HTTP native strings and the following list of Java types:
|
When specifying the type for Lists and other Collections, take care not to preinitialize your List.
The ActionContext contains all of the data available to the framework’s processing of the request, including things ranging from application data to session- or application-scoped maps. All of your application-specific data, such as properties exposed on your action, will be held in the ValueStack, one of the objects in the ActionContext.
All OGNL expressions must resolve against one of the objects contained in the ActionContext. By default, the ValueStack will be the one chosen for OGNL resolution, but you can specifically name one of the others, such as the session map, if you like.
Figure 6.1. The ActionContext holds all the important data objects pertaining to a given action invocation; OGNL can target any of them.
|
拦截器在启动服务器的时候,就已经初始化好了。