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.

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

重读《Struts In Action》的更多相关文章

  1. 关于Spring的Controller及Struts的Action的多线程的注意

    struts是线程安全,并不是指多线程,而是指单态,当多个用户访问一个请求的时候,服务器内存中只有一个与之对应的action类对象,execute方法加上了同步关键字,如果你在action里加上一个全 ...

  2. struts中action名称反复导致的神秘事件

    近期由于项目需求变更.须要本人对当中的某个业务功能进行改动.本人依照前台页面找action,依据action找代码的逻辑进行了改动(公司项目是ssh框架,struts配置全部是通过注解的方式进行.配置 ...

  3. 实现Spring管理struts的Action

    struts2和spring的整合,关键点在于struts2中的action要纳入spring容器的管理中成为一个bean.  可以在struts2中配置:  <struts>      ...

  4. (五)Struts之Action类基础(二)

    上一章节末((三)Struts之Action类基础(一))介绍了如何获取用户输入数据的获取.接着就是在Struts中怎么把数据响应给用户端,这就必须要求我们把数据放到作用域中,然后才能显示到用户浏览器 ...

  5. JavaWeb_(Struts2框架)Struts创建Action的三种方式

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  6. Struts中Action三种接收参数的方式?

    前言: 前面已经有一篇随笔介绍了Struts2的大概原理.本文就Struts2中Action与jsp页面进行数据对接时介绍几种常见方法! 值栈ValueStack 3个Action Action1 p ...

  7. struts+service+action+数据库

    用户登录流程 1.jsp根据form表单中的action的login   <form action="/test02/login" method="post&quo ...

  8. 初次了解struts的action类

    Action类真正实现应用程序的事务逻辑,它们负责处理请求.在收到请求后,ActionServlet会为这个请求选择适当的Action 如果需要,创建Action的一个实例 调用Action的perf ...

  9. struts 在Action中访问web元素(request,session等)

    出发jsp: <?xml version="1.0" encoding="GB18030" ?> <%@ page language=&quo ...

随机推荐

  1. 对C语言中va_list,va_start,va_arg和va_end的一点理解

    这几个函数和变量是针对可变参数函数的,什么是可变参数函数呢,最经典的莫过于printf和scanf,这两个函数的声明如下: int printf(const char *format, ...); i ...

  2. cmd中无法运行svn命令

    Svn 不是内部或外部命令,也不是可运行的程序 解决方法: 增加“svn安装目录/bin”,例如:C:\Program Files\TortoiseSVN\bin

  3. js----全局变量和局部变量部分讲解

    以此文作为自己学习的一个总结. 关于全局变量和局部变量的一句简单的定义:在函数外声明的变量都为全局变量,在函数内声明的为局部变量. 一.局部变量和全局变量重名会覆盖全局变量 var a = 1; fu ...

  4. decode行转列,case when,

    1.行转列 转之前:

  5. Java数字处理

    给出一个不多于5位的正整数,要求如下: (1)求出该数是几位数. (2)分别打印出每一位数字. (3)按照逆序打印出各位数值. 按照以上要求,首先得用户从键盘输入一个不多于5位的正整数,可以用Syst ...

  6. DevExpress GridControl 使用方法技巧 总结 收录整理

    一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 ().gridView.AddNe ...

  7. List集合实战总结

    //构造被分隔的集合 List<object> list = new List<object>(); for (int i = 0; i <= 100; i++) { l ...

  8. asp.net mvc razor html encoding

    HTML Encoding 为了跨站点的脚本攻击,Razor 语法会直接将脚本代码编码输出. @{string message = "<script>alert('haacked ...

  9. 03-树3 Tree Traversals Again

    二叉树及其遍历 push为前序遍历序列,pop为中序遍历序列.将题目转化为已知前序.中序,求后序. 前序GLR 中序LGR 前序第一个为G,在中序中找到G,左边为左子树L,右边为右子树R. 将左右子树 ...

  10. L2-015. 互评成绩

    学生互评作业的简单规则是这样定的:每个人的作业会被k个同学评审,得到k个成绩.系统需要去掉一个最高分和一个最低分,将剩下的分数取平均,就得到这个学生的最后成绩.本题就要求你编写这个互评系统的算分模块. ...