博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
重读《Struts In Action》
阅读量:5983 次
发布时间:2019-06-20

本文共 4965 字,大约阅读时间需要 16 分钟。

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/Booleantrue 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.

 

 拦截器在启动服务器的时候,就已经初始化好了。

转载地址:http://imeox.baihongyu.com/

你可能感兴趣的文章
基本安装lnmp环境
查看>>
logstash消费阿里云kafka消息
查看>>
Oracle——条件控制语句
查看>>
day-6 and day-7:面向对象
查看>>
CSU Double Shortest Paths 湖南省第十届省赛
查看>>
webgl像机世界
查看>>
php正则怎么使用(最全最细致)
查看>>
javascript数学运算符
查看>>
LC.155. Min Stack(非优化,两个stack 同步 + -)
查看>>
交互设计[3]--点石成金
查看>>
SCCM TP4部署Office2013
查看>>
bootloader功能介绍/时钟初始化设置/串口工作原理/内存工作原理/NandFlash工作原理...
查看>>
利用console控制台调试php代码
查看>>
讲解sed用法入门帖子
查看>>
Linux 内核已支持苹果
查看>>
【二叉树系列】二叉树课程大作业
查看>>
ASP.NET Core 2 学习笔记(三)中间件
查看>>
hbase region split源码分析
查看>>
SurfControl人工智能新突破 领跑反垃圾邮件
查看>>
一个动态ACL的案例
查看>>