您的位置:首页 > 编程语言 > Java开发

重读《Struts In Action》

2015-04-13 08:37 357 查看
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:

String— Sometimes a string is just a string.

boolean/Boolean— true and false strings can be converted to both primitive and object versions of Boolean.

char/Character— Primitive or object.

int/Integer, float/Float, long/Long, double/Double— Primitives or objects.

Date— String version will be in SHORT format of current Locale (for example, 12/10/97).

array— Each string element must be convertible to the array’s type.

List— Populated with Strings by default.

Map— Populated with Strings by default.

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.



拦截器在启动服务器的时候,就已经初始化好了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: