Working with Struts 2 actions 读书笔记

来源:互联网 发布:c 多线程编程 视频 编辑:程序博客网 时间:2024/06/06 06:43

Introducing Struts 2 actions  (介绍struts2动作相关知识 -- 角色、组件、职责、平等)


To set the stage, we’ll start with a sketch of the role that actions play in the framework.

(为了打好基础,我们详细介绍动作在框架中的扮演的角色)

We’ll contrast the Struts 2 action with the similarly named component in Struts 1.

(我们将比较Struts2的动作组件和Struts1中命名相似的动作组件)

And we’ll study the obligations that an object serving in the role of an action has toward the framework in general.

(并且我们将研究作为动作组件的对象再框架中的职责)

Struts 2 is an egalitarian enterprise.

(Struts 2是一个平等的系统)

Any class can be an action as long as it satisfies its obligations to the framework.

(只要满足了对框架该尽的职责,任何类都可以成为动作)


What does an action do?(动作要做哪些事情呢 -- 封装、数据搬运、回应请求)


First, 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.

(最后,动作必须帮助框架决定哪个结果应该呈现请求响应中返回的视图)


ACTIONS ENCAPSULATE THE UNIT OF WORK(动作封装工作单元 -- execute()、business logic、业务组件、)

Earlier in this book, we saw that the action fulfills the role of the MVC model for the framework.

(在本书前几章中,我们看到动作在框架中可作为MVC模式的模型)

The code inside this method should concern itself only with the logic of the work associated with the request.

(在这个方法内部的代码应该关注与请求相关的工作逻辑)

In this case, the business logic amounts to little more than a concatenation.

(此时,业务逻辑只需拼接字符串)

If it were much more complex, we’d probably have bumped that logic out to a business component and injected that component into the action.

(如果业务逻辑非常复杂,我们可能会把业务逻辑构建成一个业务组件,再把这个业务组件注入到动作中)


ACTIONS PROVIDE LOCUS FOR DATA TRANSFER(动作为了数据转移提供场所 -- 携带、JavaBeans properties、简洁、techniques、mechanisms、compare struts1、store)

Being the model component of the framework also means that the action is expected to carry the data around.

(动作是框架的模型组件,也就意味着动作必须能够携带数据)

There might be a bunch of JavaBeans properties adding lines of code to the action, but when the execute() method references the data in those properties, the proximity of the data makes that code all the more succinct.

(获取一系列的JavaBeans属性会增加动作组件的代码,但是当执行execute()方法引用这些属性的数据时,邻近的数据会让动作的代码变得更简洁)

In addition to receiving the incoming data from the request, these JavaBeans properties on the action will also expose the data to the result.

(除了从请求接收传入数据,这些在动作上的JavaBeans属性将作为结果把这些数据公开)

The HelloWorld action’s logic sets the custom greeting on the customGreeting property, which makes it available to the result as well.

(HelloWorld动作逻辑的将顾客的问候放置在customGreeting属性上,使它很好的展现在结果上)

In addition to these simple JavaBeans properties, there are a couple of other techniques for using the action as a data transfer object.

(除了这些简单的JavaBeans属性,为了使用动作作为数据传输对象,用到了其它别的两种技术)

We’ll examine these alternatives later in this chapter, and will also examine the mechanisms by which the actual data transfer occurs.

(我们将在本章后面会介绍这两种方法,同样也会通过实际数据传输来介绍机制)

For now, we just want to recognize that the action serves as a centralized data transfer object that can be used to make the application data available in all tiers of the framework.

(现在,只是让你认识到,动作作为一个集中的数据转移对象,可以让应用程序的数据在框架的所有层次可用)

The use of actions as data transfer objects should probably ring some alarms in the minds of alert Struts 1 developers.

(用动作作为传输数据的对象应该会引起struts1开发者的警觉)

If this were still true, we couldn’t use the action object itself as a data carrier for the request.

(如果strtus 2也是这样,就不能将动作自身作为请求的数据携带者)

This fundamental difference allows Struts 2 objects to exist as dedicated data transfer objects for each request.

(就因为他们本质的不同,才使得Struts 2对象针对每个请求专有的数据转移对象)


ACTIONS RETURN CONTROL STRING FOR RESULT ROUTING (动作为结果路线返回控制范围内的字符串 -- return、routing、must return string)

The final duty of an action component is to return a control string that selects the result that should be rendered.

(动作组件的最后一个职责是返回一个控制字符串,这个控制字符串其实是应该呈现到结果页面)

Previous frameworks passed routing objects into the entry method of the action.

(先前框架将规划好路线的对象传入动作方法的入口)

Returning a control string eliminates the need for these objects, resulting in a cleaner signature and an action that is less coupled to specific routing code.

(返回的控制字符串消除了对这些对象的依赖,使得方法签名更加简洁,并且降低了动作与具体路由代码的耦合)



Packaging your actions(打包动作  --  inherit、portfolio、two distinct region、)

They provide a mechanism for grouping your actions based on commonality of function or domain.

(针对动作分组,Struts 2提供了一种基于功能或领域的共性机制)

Many important operational attributes, such as the URL namespace to which actions will be mapped, are defined at the package level. 

(一些重要的操作属性,如果用映射到动作的URL来命名空间,都是在包级别定义的)

And, importantly, packages provide a mechanism for inheritance, which among other things allows you to inherit the components already defined by the framework.

(并且,更重要的是,包能提供一种继承机制,除了其它的特性之外还让你能够继承框架已经定义的组件)

In this section, we’ll check out the details of Struts 2 packages by examining the Struts 2 Portfolio application’s packaging.

(在这个章节中,我们会开发和研究一个Struts 2公文包的示例应用程序)

Artists can use the application to create an online portfolio of their work.

(艺术家可以使用这个应用程序创建一个他们作品的在线公文包)

The portfolio is a simple gallery of images.

(文公文是一个简单的图片库)

Artists must first register with the system to have a portfolio.

(艺术家必须现在系统中注册才能得到一个公文包)

It’s free, but we’ll collect some harmless personal information.

(公文包是免费的,但是我们可以收集一些个性化的信息)

Once the artist has an account, she can log in to the secure portion of the application to conduct such sensitive business as creating new portfolios, as well as adding and deleting images from those portfolios.

(艺术家在拥有账户后,就能登录这个应用程序的安全区域进行一些敏感的操作,例如创建新的公文包,想公文包中添加图片或从公文包中删除图片)

The other side of the portfolio is the public side.

(Struts 2公文包应用程序的另一个区域是公共的)

A visitor to the public site can view the images in any of the portfolios.

(公共区域的访问者可以查看任何公文包中的图片)

This public face of the portfolio won’t be protected by security.

(公共部分没有安全保护,不需要登录)


Organizing your packages(组织你的包  --  declare、configure、name、namespace、extends、abstract)

We’ll organize the Struts 2 Portfolio’s packages based upon commonality of functionality, a common strategy.

(我们根据功能的共通性来组织Struts 2公文包应用程序的包,这也是最常用的策略)

A glance at the names of these actions should be sufficient to give a good idea of their functional purpose.

(扫一眼这些动作的名字就可以了解这些动作的功能)

We want to make sure that the user who’s removing images from the portfolio actually owns that portfolio.

(我们想确认正在从公文包中移除图片的用户确实是当前公文包的拥有者)

Grouping these together allows us to share declarations of components that might be useful to our authentication mechanism.

(把这些动作分组在一起可以共享对用户身份验证机制有用的组件声明)

While it may be challenging to choose the strategy by which you divide your actions into packages, declaring them is simple.

(虽然决定将动作分在不同包内的策略很有挑战,但是声明他们非常简单)

The name attribute is merely a logical name by which you can reference the package.

(name属性仅仅是一个逻辑名,通过它可以引用这个包)

We’ve used the Struts 2 packaging mechanism to isolate these versions from one another.

(我们使用Struts 2包机制来隔离这些不同的版本)

Not only does this allow us to offer versions of the Struts 2 Portfolio that focus on the specific goals of each chapter, thus decreasing the learning curve, it also serves to further demonstrate the usefulness of packages.

(这不但让我们能够关注每一章的特定目的,提供不同版本的Struts 2公文包应用程序(这样能够降低学习曲线),而且更深入地展示了包的用途)

The default namespace sits beneath all of the other namespaces waiting to resolve requests that don’t match any explicit namespace.

(默认命名空间在其它所有的命名空间之下,用来解决不能与任何显示声明的命名空间匹配的请求)

It’s important to distinguish between the empty default namespace, which can catch all request patterns as long as the action name matches, and the root namespace, which is an actual namespace that must be matched.

(区分空的默认命名空间和根命名空间非常重要,在空的默认的命名空间中,动作名字匹配的所有请求模式都可以被捕获,而根命名空间是一个真实的命名空间,必须要完全匹配)

Most of the intelligent defaults are defined in a built-in package called struts-default.

(大多数默认值都内建在一个叫做struts-default的包中)


Using the components of the struts-default package (讲述如何来使用struts-default包  --  interceptor)

 declares a huge set of commonly needed Struts 2 components ranging from complete interceptor stacks to all the common result types.

(struts-default声明了大量常用的struts 2组件,从完整的拦截器栈到常用的结果类型)

This important params interceptor has been the one moving data from the request parameters to our action’s JavaBeans properties.

(这个重要的params拦截器负责将数据从请求参数转移到动作的JavaBeans属性中)

The work is getting done with good old-fashioned lines of code. For the curious, these specific lines of code provide good insight into the inner workings of Struts 2;

(这些工作由编写的很好的普通代码完成。对于好奇的人来说,具体的代码可以让你让深入理解Struts 2的内部工作,)

While you’re not required to extend the struts-default package when you create your own packages, omitting this bit of inheritance amounts to rejecting the core of the framework.

(虽然你创建自定义的包时不一定要扩展struts-default包,但是省略了继承就等于拒绝了框架的核心功能)


Implementing actions(实现动作  --  就算不实现Action接口,也会有一个默认的execute方法默认返回一个SUCCESS字符串常量)

The optional Action interface (可选的Action接口)

Struts 2 gives developers both a fast development path built on intelligent defaults and an extremely high degree of flexibility to elegantly solve the most arcane use cases.

(Struts 2既为开发人员提供了一个基于智能默认值的快速开发路径,又提供了高度的灵活性来优雅的解决最晦涩的案例)

Without a good understanding of the straight and narrow, the framework’s flexibility can admittedly leave one feeling concerned about which path to follow.

(在适应了问题的常规方法后,框架的灵活性会变得很自然、很强大。不能很好的理解直观的、简单的方法、框架的灵活性肯定会让你不知所措)

Since the framework doesn’t make any type requirements, you could just put the method in your class without having your class implement this interface.

(由于框架没有任何类型要求,你能在你的类不实现action接口的情况下设置方法)

This means that using these predefined control strings allows you to tap into even more intelligent default behavior.

(这意味着使用这些预定义的控制字符串允许你接入更多的智能默认行为)

Since our sole result forgoes defining its own name, it inherits this default and is automatically selected by our action.

(因为结果没有定义自己的名字,它继承了智能默认值并且自动被动作选择)

This is the general pattern by which many of the intelligent defaults operate.

(这是许多智能默认值默认选择的方式)


The ActionSupport class (ActionSupport 类  --  data validation、localization of error messages)

This convenience class is a perfect example of the Struts 2 straight and narrow we spoke of a bit earlier.

(这个类的便利性是前面说的Struts 2简单方法的一个完美示例)


BASIC VALIDATION(基本验证  --  validation actually work、基本验证是通过继承:struts-default 和 ActionSupport)


The workflow interceptor must fire after the params interceptor has had a chance to move the data on to the action object.

(workflow拦截器必须在params拦截器将数据从请求转移到动作对象之后触发)

When we submit the form, the validation fails and diverts the workflow back to the input form again, as seen in figure 3.2.

(当我们提交表单的时候,如果验证失败就会使得这个工作流再次返回输入表单,就像图3-2看到的一样)

Where were those error messages stored, and how did the workflow interceptor check to see whether any had been created?

(这些错误信息保存在哪里,workflow拦截器如何检查是否有错误消息被创建?)

A class that implements this important interface must maintain a collection of error messages for each field that can be validated, as well as a collection of general error messages that pertain only to the action as a whole.

(实现这个重要接口的类必须为每一个可以被验证的字段维护一系列错误消息和一系列与动作整体相关的通用错误信息)

Adding an action-scoped error message is even easier, as you don’t need to specify anything other than the message.

(添加一个动作范围内的错误消息更加简单,只需要指定错误消息)

A more subtle point is that the control flow of the validation process is also separated from the action.

(更重要的是,验证过程的控制流也从动作中分离出去了)

The validation workflow is itself layered away from the action’s workflow because the validation logic is invoked by the workflow interceptor.

(验证的工作流也从动作的工作流中分层出来了,因为验证逻辑被工作流拦截器调用)

This separation of control flow is what allows the workflow interceptor to abort the whole request processing and redirect back to the input page without ever entering the action’s execute() method.

(控制流的分离使得workflow拦截器在进入动作的execute()方法之前可以中止整个请求处理并且重定向回输入页面。)

This is exactly the kind of separation that interceptors are meant to provide.

(这正是拦截器提供的分离类型)

Before moving on, some of you are probably wondering how the error message made its way onto the registration form when we sent the user back to try again.

(在继续开发之前,有些开发人员可能想知道,当返回输入页让用户再尝试时,错误信息是如何进入注册表单的)

All of this is handled for you with the Struts 2 UI component tags.

(所有这些内容都由Struts 2 UI标签完成)

Now it’s beginning to feel like we’ve really learned something.

(现在开始确实觉得已经学到了东西)

That’s cool, but let’s not get distracted from the real topic at hand.

(很酷,但是不要被手边的主题分心)

The real lesson to take out of this section is about how actions work together with interceptors to get common chores done without polluting the action’s core logic.

(本章真正要讲述的内容是,动作如何与拦截器协作,在不污染动作核心逻辑的情况下完成常见任务。)

If you can wrap your mind around this action/interceptor teamwork, then you’ll find the rest of the book merely an elaboration on that theme.

(如果你完全理解了动作/拦截器这个组合,你就会发现本书剩下的内容只不过是这个主题的细化)


USING RESOURCE BUNDLES FOR MESSAGE TEXT (使用资源包处理文本信息  --  TextProvider、LocaleProvider)

Furthermore, changing languages for different user locales is virtually impossible without some layer of separation between the source code and the messages themselves.

(另外,如果不把源代码和消息本身分开开来,那么为来自不同地域的用户改变语言几乎不可能)

The well-established best practice is to bundle these messages together into external and maintainable resource bundles, commonly implemented with simple properties files.

(广为接受的最佳实践是将这些消息集中放入外部可维护的资源包中,通常使简单的文件实现)

ActionSupport implements two interfaces that work together to provide this localized message text functionality.

(ActionSupport 实现了两个接口,它们协作提供了本地化消息文本功能)

This interface exposes a flexible set of methods by which you can retrieve a message text from a resource bundle.

(这个接口提供了一系列灵活的方法,使用这些方法可以从资源包中取得)

The TextProvider methods will return the message associated with that key from the properties file associated with your action class.

(TextProvider方法会从与动作类相关的属性文件中返回与这个关键字对应的消息)

This layer of separation makes our message text much more manageable.

(这种分层使得消息文件更加可管理)

ActionSupport implements this interface to retrieve the user’s locale based upon the locale setting sent in by the browser

(根据浏览器发送来的地域设置取得用户所在的地域)

Even when we weren’t taking advantage of it, ActionSupport’s TextProvider implementation has been checking the locale every time it retrieves a message text for us.

(甚至当我们还没有使用它的时候,ActionSupport的TextProvider实现已经开始在每次为我们取得文本消息的时候检查地域)

Of course, you have to provide the properties file for the locale in question, or it will just serve up the standard English.

(当然,你需要为当前地域提供属性文件,否则只会呈上标准英语)


Transferring data onto objects(向对象传递数据  --  )

Struts 1 to Struts 2 Perspective.

(从Struts 1 看 Struts 2)

For each domain object, you typically had to create a mirroring form bean.

(每一个域对象通常情况下都需要创建一个对等的ActionForm对象)

To add insult to injury, you were then tasked with an additional manual data transfer when you finally moved the valid data from the form bean onto your domain object.

(更糟糕的是,当你最终把合法的数据从ActionForm转移到域对象时,还有额外的手工数据转移的任务等着你)



Object-backed JavaBeans properties (对象支持的JavaBean属性  --  到底是个什么意思)

To enable this transfer, the developer needs only to provide JavaBeans properties on her actions, using the same names as the form fields being submitted.

(为了能够自动传输数据,开发人员只需要在动作上提供JavaBeans属性,并且使用相同的名字作为被提交的表单的字段)

This tedious task consists of collecting these individually transferred data items and transferring them to an application domain object that we must instantiate ourselves.

(这项繁琐的任务包括收集转移的独立数据元素,并且将他们转移到必须我们自己实例化的应用程序领域)

While we were impressed with the succinct quality of this method only a few pages ago, we can now see that five of the seven lines do nothing more than assemble the individual pieces of data ① that the framework has transferred onto our simple Java- Beans properties.

(虽然就前面的内容中,这个方法的简洁特性给我们留下了深刻的印象,现在我们可以看到6行代码中的4行只是将框架转移来的数据装配到简单的JavaBean属性上。)

We’re still psyched that the data has been automatically transferred and bound to our Java data types, but why not ask for more?

(我们仍然很兴奋,数据被自动转移并且被绑定到了Java数据类型,但是为什么不要求更多呢?)

Why not ask the framework to go ahead and transfer the data directly to our User object?

(为什么不让框架继续前行,把数据直接转移到User对象上呢?)






0 0
原创粉丝点击