Saying hello to Struts 2 读书笔记

provides the practical and concrete details to bring the theoretical concepts from the first chapter down to earth.

(本章将通过实践和具体的细节来把第1章中讲述的理论概念落到实处)

This application isn’t intended to demonstrate the full complexity of the framework.

(这个示例应用程序并不能展示这个框架的全部功能)


Declarative architecture

In this book, we use the phrase declarative architecture to refer to a type of configuration that allows developers to describe their application architecture at a higher level than direct programmatic manipulation.

(在本书中,我们使用声明性架构来引用一种配置,这种配置允许开发者在高级别上描述他们的应用型架构,而不是直接编程操作)

Similar to how an HTML document simply describes its components and leaves the creation of their runtime instances to the browser, Struts 2 allows you to describe your architectural components through its high-level declarative architecture facility and leave the runtime creation of your application to the framework.

(和HTML文档简单描述它的组件类似,而将创建这些运行时实例的任务交给了浏览器。struts2允许你通过高级的声明架构组件来描述应用程序的架构组件,而将运行时创建这些架构组件的任务交给框架)


Two kinds of configuration

First, we need to clarify some terminology.

(首先,我们需要明白一些科学术语)

In the introduction to this chapter, we referred to the act of declaring your application’s Struts 2 components as configuration.

(在本章的介绍部分,我们把声明应用的struts2组件的行为称为配置)

While there is nothing wrong with this nomenclature, it can be confusing.

(虽然这么命名没有什么错误,但是它可能会引发一些困惑)

Hidden beneath the far-reaching concept of configuration, we can distinguish between two distinct sets of activity that occur in a Struts 2 project.

(在这个意义深远的配置概念之后,在struts2项目内部我们可以区分两类明显不同的行为)

One of these, the declarative architecture, is more central to actually building Struts 2 applications, while the other is more administrative in nature.

(其中一类是声明性架构,与实际构建struts2应用程序更接近,而另外一类在本质上更接近于管理。)

Conceptually, it’s important to distinguish between the two.

(在本节中,我们将会了解上述过程的细节)


CONFIGURING THE FRAMEWORK ITSELF

First, we have configuration in the traditional sense of the word.

(首先,我们理解传统意义上的配置)

These are the more administrative activities.

(这而配置更多的是管理上的活动)

Because the Struts 2 framework is flexible, it allows you to tweak its behavior in many areas.

(因为struts2框架是非常灵活的,它允许你在许多区域调整它的行为)

If you want to change the URL extension by which the framework recognizes which requests it should handle, you can configure the framework to recognize any extension that you like.

(如果你想改动框架赖以识别哪些请求应该处理的URL扩展名,那么可以配置框架去识别你所用的扩展名)

By default, Struts 2 looks for URLs ending in .action, but you could configure the framework to look for .do ( the Struts 1.x default extension) or even no extension at all.

(默认情况下,struts2查找以.action结尾的URL,但是你可以配置框架查找.do(struts1的默认扩展名),甚至可以不适用任何扩展名)

Other examples of configurable parameters include maximum file upload size and the development mode flag.

(其它配置参数还包括上传文件的最大容量和开发模式标志等)

Due to its administrative nature, we’ll explain this type of configuration as we come to topics in the book that can be configured.

(由于这些配置属于管理性质,我们会在本书中遇到配置的主题时再讲解)


DECLARING YOUR APPLICATION’S ARCHITECTURE(struts2 比较简单,只需要实现一些动作)

The more important type of configuration, which we’ll refer to as declarative architecture, involves defining the Struts 2 components that your application will use and linking them together—or wiring them—to form your required workflow paths.

(这种类型的配置更重要,我们称之为声明性架构,包含定义应用程序使用的struts2组件,以及将他们的连接起来形成需要的工作流路径)

By workflow path, we mean which action fires when a particular URL is hit, and which results might be chosen by that action to complete processing.

(这里说的工作流路径是指当一个特定的URL被单击时会触发哪个动作,以及该动作会选择哪个结果来完成处理流程)

Declarative architecture is a specialized type of configuration that allows developers to create an application’s architecture through description rather than programmatic intervention.

(声明性架构是一种特别的配置方式,这种类型允许开发者通过描述而不是硬编码的方式来创建一种应用型程序的结构)

The developer describes the architectural components in high-level artifacts, such as XML files or Java annotations, from which the system will create the runtime instance of the application.

(开发人员使用高级工件来描述架构组件,例如:XML文件或java注解,系统会应用高级化组件来创建应用程序的运行时实例)

The developer needs only to declare which objects will serve as the actions, results, and interceptors of their application.

(开发人员仅仅需要声明哪些对象作为应用程序的动作、结果和拦截器)

This process of declaration primarily consists of specifying which Java class will implement the necessary interface. Almost all of the Struts 2 architectural components are defined as interfaces.

(这个声明过程主要包括哪个java类实现哪个接口。几乎所有的struts2组件都被定义为接口)

In reality, the framework provides implementations for nearly all of the components you’ll ever need to use.

(实际上,框架提供了你可能用到的所有的组件实现)

For instance, the framework comes with implementations of results to handle many types of viewlayer technologies.

(例如,框架包含了很多结果的实现,以支持不同类型的显示技术)

Typically, a developer will only need to implement actions and wire them to built-in results and interceptors.

(一般说来,开发者仅仅需要实现动作,之后将他们内建的结果和拦截器关联起来)

Furthermore, the use of intelligent defaults and annotations can further reduce the manual tasks needed in this area.

(此外,使用智能默认值和注解可以 进一步减少配置过程中所需要的手工任务)


Two mechanisms for declaring your architecture(声明你架构的两种方式:xml 和 annotation)

As you can see, whether your application’s Struts 2 components are declared in XML or in annotations, the framework translates them into the same runtime components.

(如你所看到的,不管使用XML方式还是java注解方式声明应用程序的struts2组件,框架都会将他们转化为相同的运行组件)

In the case of the XML, we have the familiar XML configuration document with elements describing your application’s actions, results, and interceptors.

(在XML例子中,我们使用带有描述应用程序的动作、结果和拦截器等元素的XML配置文档)

Now, the metadata is collected in Java annotations that reside directly within the Java source for the classes that implement your actions.

(所有的元数据都集中放在java注解中,这些注解直接驻留在实现动作的类对应的java源代码中)

Regardless of which method you use, the framework produces the same runtime application.

(无论你使用什么方法,框架都会产生同样的运行时应用程序)
The two mechanisms are redundant in the sense that you can use whichever you like without functional consequence.
(你可以使用自己喜欢的配置方式,它们没有功能上的优先顺序,从这种意义上来说两种机制是冗余的)
Which style of declaration you choose is largely a matter of taste.
(选择哪种声明的样式要比主要取决于你的偏好)

XML-BASED DECLARATIVE ARCHITECTURE(基于XML的声明性架构)
Many of you are already familiar with the use ofXMLfor declarative software development.
(和许多使用XML用来声明软件开发类似)
We just show this as an example of what XML-style declarative architecture looks like. 
(仅展示它作为一个XML风格的声明性架构看起来是什么样子)
Typically, an application will have severalXMLfiles containing elements like these that describe all of the components of the application. 
(通常情况下,一个应用将有几个XML文件,但是所有的应用组件都是由这些描述的元素组成)
Even though most applications will have more than oneXMLfile, all of the files work together as one large description.
(即使多数应用有更多的XML文件,而这些文件就像大型描述的一样一起工作)
The framework uses a specific file as the entry point into this large description.
(框架使用一个特别的文件作为进入这个大的描述的入口点)
This entry point is the struts.xml file.
(这个入口点就是struts.xml文件)
This file, which resides on the Java classpath, must be created by the developer.
(这个文件存放在类路径上,必须通过开发者来创建)
While it’s possible to declare all your components in struts.xml, developers more commonly use this file only to include secondary XML files in order to modularize their applications.
(当你在struts.xml定义你所有的组件时,为了应用程序模块化,开发人员只用这个文件包含次级的XML文件)
We’ll see XML-based declarative architecture in action when we look at the Hello-World application in a few moments.
(在稍后介绍Hello-World应用程序时,我们会看到基于XML的声明性架构)

JAVA ANNOTATION–BASED DECLARATIVE ARCHITECTURE(java基于注解的声明性架构)
A relatively new feature of the Java language, annotations allow you to add metadata directly to Java source files.
(它是java语言的一个新特性,注解允许你添加元数据来直接访问java源文件)
One of the loftier goals of Java annotations is support for tools that can read metadata from a Java class and do something useful with that information.
(java注解的一个更高级的用途是为了支持一种工具,这种工具能从一个java类中读取元素据,并且使这些信息做一些有用事情)
We’ll explain how the framework finds the classes that it should scan for annotations when we demo the annotated version of HelloWorld, which we’ll do shortly. Listing 2.2 shows what these annotations look like.
(当我们展示注解版本的HelloWorld应用程序时,会解释框架如何找到应该扫描注解的java类,稍后我们马上介绍。代码清单2-2展示了这些注解)
Note that the annotations are made on the Java classes that implement the actions.
(注意,这些注解必须写在那些实现动作的java类上)
Listing 2.2 shows the code from the Login class, which itself will serve as the Login action.
(列表2.2表明来自登录的类代码,它本身将服务于登录的动作)
Just like their counterpart elements in the XML, these annotations contain metadata that the framework uses to create the runtime components of your application.
(就像与之对应的XML元素一样,这些注解包含了框架用来创建应用程序组件的元数据)
We’ll fully explain this material throughout the course of the book, but it might be worthwhile to note the relationship between the Login action’s XML element in listing 2.1 and the annotations of listing 2.2, which are made directly on the Login.java source itself.
(我们将通过这本书来深入的讲解这些内容, 并且读者很有必要去了解去login动作在代码清单2-1中的XML元素和在代码清单2-2中的注解之间的关系)
The annotation-based mechanism is considered by many to be a more elegant solution than the XML mechanism.
(基于注解的配置相对于xml配置更优雅的解决方案)
For one thing, the annotation mechanism is heavily combined with convention-based deduction of information.
(最起码,注解机制与基于约定的信息紧密相连)
In other words, some of the information that must be explicitly specified in the XML elements can be deduced automatically from the Java package structure to which the annotated classes belong.
(换句话说,有些在XML中必须显示指定的信息,可以从注解的类所隶属的java包结构自动推断出来)
For instance, you don’t need to specify the name of the Java class, as that is clearly implicit in the physical location of the annotations.
(例如,你不需要指定的java类的名字,因为它明确隐含注解的物理位置中)
Many developers also appreciate how annotations eliminate some of the XML file clutter that seems to increase year by year on the web application classpath.
(许多开发者还很欣赏注解减少了多年以来在web应用程序类路径下积累的XML文件的混乱状况)
We’ll demonstrate the fundamentals of annotation-based declarative architecture in the second version of the HelloWorld application provided later in this chapter.
(在本章后面会提供第二版HelloWorld应用,展示基于注解的声明性架构


WHICH METHOD SHOULD YOU USE?(你该使用哪一种呢?
Ultimately, choosing a mechanism for declaring architecture is up to the developer.
(最终选择哪种方式来声明架构由开发人员决定)
The most important thing is to understand the concepts of the Struts 2 declarative architecture.
(对于理解struts2声明性架构来说,最重要的是理解这些概念)
If you understand those, moving between the XML or Java annotation– based mechanisms should be quite trivial.
(如果你理解这些概念,那么就可以轻松的在XML和java注解机制之间转换)
The Struts 2 developers are ardently moving toward a zero-configuration system that uses convention over configuration, with annotations serving as an elegant override mechanism when conventions aren’t followed.
(struts2的开发人员正在热切的朝着零配置系统努力,在不需要遵守约定时,java注解充当覆盖这些约定的机制)
Many people are already using this system, but we think at this point it isn’t the best approach to learning the framework.
(虽然许多人都准备来使用这个系统,但是我们认为在这个点上并不是最好的时机来学习这个框架)
We do think that many of you will ultimately choose to use Java annotations to declare your application’s components because of their elegance.
(我们认为你最终会因为它的有雅兴而选择使用java注解来声明你的应用组件)

Intelligent defaults(多数组件都已经被框架进行了声明)
Many commonly used Struts 2 components (or attributes of components) do not need to be declared by the developer.
(大多数开发者使用struts2组件都不需要声明)
Regardless of which declaration style you choose, these components and attribute settings are already declared by the framework so that you can more quickly implement the most common portions of application functionality.
(不管你选择哪种声明方式,这些组件和属性设置已经通过框架声明,以便开发人员能够更快的实现应用程序功能的常用部分)
Intelligent defaults provide out-of-the-box components that solve common domain workflows without requiring further configuration by the developer, allowing the most common application tasks to be realized with minimum development.
(智能默认值提供了内置的组件,在不需要开发人员更多配置的情况下,就可以解决常见的领域工作流,从而允许以最小的开发工作量实现常见的应用程序功能)
These predefined components are part of the Struts 2 intelligent defaults.
(这些预先定义组件是struts2智能化默认值的一部分)

A quick hello
Then we’ll revisit the same application implemented with annotations, focusing on the annotations themselves.
(之后我们重新审视使用注解实现的同一个应用程序,届时将重点放在注解上)
The two HelloWorld applications, which differ only in the style of architectural declaration, will make this point concrete.
(两个只有架构声明风格不同的HelloWorld应用程序正好说明了这个事实)

Deploying the sample application(部署简单的应用)
Furthermore, we think the benefits gained from learning to install a servlet container far outweigh any short-term gains to be had from any container-specific quick start we might try to provide.
(此外,我们认为学会安装servelt容器要比阅读任何特定容器的快速安装指南能获得更多的知识。)
If you’re experienced with Java web application development, you’ll already have your own container preferences and know how to deploy a web application in your chosen container.
(如果你在java web应用开发上有经验,那么你已经有了自己喜欢的servlet容器,并且已经知道如何在你选择的容器中部署应用程序)
If you’re new to Java web application development, you can probably expect to spend a few hours reading some online documentation and working through the installation process.
(如果你在java web应用开发上是新手,你可能需要花费一些时间来阅读在线文档和安装工作)
Choosing a servlet container can be overwhelming, but for newbies we recommend Apache Tomcat.
(对于如何选择servlet容器,对于我们新手来说我们推荐Apache Tomcat)
It’s arguably the most popular open source implementation of the Servlet Specification.
(它无疑是最流行的servlet规范的开源实现)
It’s both easy to obtain and certain to be as specified.
(读者可以非常容易的下载到Apache Tomcat,并且获得详细说明)
Though perhaps less fundamental than the choice of a servlet container, choosing an IDE and a build tool can be just as important.
(相对于选择一个servlet容器来说,选择一个合适的IDE和一个构建工具或许不是根本问题,但也同样重要)
Our goal is to provide build- and IDE-agnostic sample applications.
(我们的目标就是提供一个和IDE、构建工具无关的示例应用程序)
We recognize that we might save you some time by providing an Ant build file with Tomcat targets, for instance, but, if you don’t use Ant and Tomcat, that doesn’t help and may even hinder your progress.
(我们发现,我们通过提供一种ant构建文件可能会给节省一些时间,这种构建是以tomcat为目标的,例如,如果你不使用ant和tomcat,那么这不但不会帮助你,甚至会干扰你的进程)
We should note that the Struts 2 community, along with much of the Java open source community, has strongly adopted Maven 2 as their build/project management tool.
(我们应该说明,像很多java开源社区一样,struts2社区一直采用maven2作为其构建项目和管理的工具)
If you plan to have more than a fleeting relationship with the Struts 2 source code, a working knowledge of Maven practice would serve you well.
(如果你计划更多的研究struts2源码,maven的实际经验可能会很好的帮到你)
Provided you have a servlet container, the only thing left to do is deploy the sample application WAR file in accordance with the requirements of your container.
(假如你已经有了servlet容器,接下来要做的就是根据容器的要求来部署示例应用程序的war文件了)
You can obtain the sample application from the Manning web site.
(你能够从manning网站上获得简单的示例应用程序)
the sample application has been organized into a series of mini-applications.
(示例应用程序被组织在一些列迷你应用程序中)
Basically, we have two versions of the HelloWorld application and many versions of the Struts 2 Portfolio application.
(基本数来,我们有两个版本的HelloWorld应用和许多不同版本的struts2应用程序)
However, the flexibility of the framework allows us to cleanly modularize the sample code for all of the chapters so that we can present distinct versions of the application for each chapter.
(然而,框架的灵活性允许我们将所有章节的示例代码都整洁的模块化,以便我们可以为每一章提供不同版本的应用程序)
This allows us to, for instance, present a simple version of the Struts 2 Portfolio while covering the basics in early chapters, and then provide a full-featured version for later chapters.
(这样,我们可以提供涵盖之前章节基本内容的公文包应用程序的一个简单版本,之后为后续章节提供一个全功能的版本)
As we go to press, we’ve responded to feedback from our Manning Early Access Program (MEAP) readers by adding a separate WAR file version of the HelloWorld sample application.
(当本书付梓之前,我们听取了本书预读项目参与者的建议,追加了一个独立的HelloWorld示例应用程序的war文件)
To fulfill both these requests, we’ve broken the XML-based HelloWorld out into a standalone web application.
(为了满足这两部分人的需求,我们把基于XML的HelloWorld应用程序分离出来做成一个单独的应用程序)
Accordingly, you’ll find HelloWorld.war also bundled with the downloadable code on the Manning site.
(因此,你可以返现在Manning网站上可下载的代码中包括HelloWorld.war文件)
This allows you to deploy the HelloWorld application without configuring the database, or other resources, that the full application depends upon.
(这让你在未配置完整的应用程序依赖的数据库和其它别的资源的情况下允许你部署HelloWorld应用程序)
Also, it provides you with a perfect skeleton application for jumpstarting your own projects.
(另外,它还提供了一个完美的基础结构,便于开始构建你自己的应用)


THE LAYOUT OF A STRUTS 2 WEB APPLICATION
The entire Struts2InAction.war file can be used as a template for understanding what’s required of a Struts 2 web application.
(整个Struts2InAction.war用来作为一种理解struts2 web应用需求的模板)
Most of the requirements for the application structure come from the requirements put on all web applications by the Servlet API.
(应用程序结构的大多数需求来源于servlet API对所有web应用程序施加的需求。)
First, all of the top-level directories, exceptWEB-INF, are in the document root of the web application. 
(首先,所有的第一层目录,除了WEB-INF之外,都在web应用程序的文档根目录中。)
You can also put Velocity and FreeMarker templates here, as we’ll do, but those resources can also load from JAR files on the classpath.
(你也能够在这些目录中放置FreeMarker或Velocity的模板文件,我们就是这样做的,当然这些资源也可以从类路径下的JAR文件中加载)
One important thing to note about the document root is that these resources can potentially be served as static resources by the servlet container.
(关于文档根目录需要注意的一项非常重要的事情是,其中的资源可以被servlet容器潜在地当做静态内容)
If not configured to prevent such access, a URL that directly points to resources in the document root can result in the servlet container spitting out that resource.
(如果不对这种访问加以保护,一个直接指向文档根目录中资源的URL可能会导致servlet容器将这个资源送出)
Because of this, the document root is not considered a secure place for sensitive material.
(就因为这个,这个文档的根目录不是存放敏感信息的安全场所)
As for the other two directories, they’re essential.
()
The lib directory holds all of the JAR file dependencies that your application needs.
(这个lib目录放的所有你应用需要的jar文件)
In the sample application, we’ve placed all of the JARs commonly used by Struts 2 web applications. 
(在示例应用中,我们放置了所有struts2应用程序常用的JAR文件)
If you add some features that we don’t use in this book, you might need to add additional JARs.
(如果你添加一些我们在本书没有使用到的特性,你可能需要添加额外的jar文件)
Also note that, if you want to see the absolute minimum set of JARs, you should check out HelloWorld.war, referenced in an earlier sidebar.
(同样需要注意的是,如果你想查看struts2必须的最小的jar文件集合,可以参考之前讲解的HelloWorld.war文件)
The classes directory holds all of the Java classes that your application will use.
(这个类目录放了你应用将使用到的所有java类文件)
These are essentially no different than the resources in the lib directory, but the classes directory contains an exploded directory structure containing the class files, no JARs.
(在lib目录中他们和原资源没有本质的区别,但是类目录的目录结果只包含class文件,没有JAR文件)
In addition to the lib and classes directories, WEB-INF also contains the central configuration file of all web applications, web.xml.
(除了lib和classes目录之外,WEB-INF也包含所有web应用程序中心的配置文件,web.xml)
This file, formally known as the deployment descriptor, contains definitions of all of the servlets, servlet filters, and other Servlet API components contained in this web application.
(这个文件的正式名称是部署描述符,它包含所有servlets定义,servlet过滤器和其它servlet API组件的定义。)
This filter will examine all the incoming requests looking for requests that target Struts 2 actions.
(这个拦截器将检查所有的请求,从而寻找出以Struts2动作为目标的请求)
The other important thing about the configuration of the Filter-Dispatcher is the initialization parameter that we pass in.
(另外一个更重要的是关于我们传入的初始化参数用来配置Filter-Dispatcher)
The actionPackages parameter is necessary if you’re going to use annotations in your application.
(如果你将在你的应用中使用注解,那么必须要设置actionPackages这个初始化参数)
It tells the framework which packages to scan for annotations.
(它将告诉框架在哪些包中扫描注解)
We’ll see more about this when we get to the annotated version of HelloWorld in a few pages.
(在稍后讲解到注解版本的HelloWorld应用程序时我们会详细探讨)
One other interesting thing to note is that we’ve included a non-Struts 2 servlet in our web application.
(另外一个需要说明的有趣地方是,在这个web应用中,我们包括一个非struts2的servlet)
As we said earlier, a web application is defined as a group of servlets packaged together.
(像我们早期说的,一个web应用程序被定义为打包在一起的一系列servlets)
Many Struts 2 web applications won’t have any other servlets in them.
(所以许多Struts2 web应用程序将不会有包含任何servlets)
In fact, since Struts 2 uses a servlet filter rather than a servlet, many Struts 2 applications won’t have any servlets in them—unless you count compiled JSPs.
(事实上,由于Struts2 使用servlet拦截器而不是servlet,许多Struts2应用程序将不会包含任何servlets -- 除非你JSP编译得到的servlet也算)
Since it’s not uncommon to integrate other servlets with the framework, we’ve included another servlet, as seen in listing 2.3, in our web application.
(由于整合框架和其它的servlets并不罕见,所以我们在web应用程序包含了另一个servlet,如清单代码2-3)
Everything in our sample application is by the book except for the presence of the source directory in WEB-INF.
(在我们示例代码中的所有内容都根据本书而来,除了WEB-INF下面的src目录)
As we said, this has been done as a convenience. You’ll probably want to structure your build according to industry best practices.
(如前面所述,这主要是为了方便。你很可能会按照业界最佳实践建立你的结构。)
We haven’t provided such a build because we think it only complicates the learning curve.
(我们并没有提供这样一种结构,是因为它只是让学习曲线更加复杂了)

Exploring the HelloWorld application
The HelloWorld application aims to provide the simplest possible introduction to Struts 2.
(HelloWorld应用程序的目的是为了提供一个最简单的方式来介绍Struts2)
However, it also tries to exercise all of the core Struts 2 architectural components.
(然而,它也尝试使用了Struts 2所有核心建构组件)
It’ll collect a user’s name from a form, use that to build a custom greeting for the user, and then present the user with a web page that displays the customized greeting.
(它将通过表格的方式来填写用户名,用这个姓名为用户构建一个自定义问候语,最后通过web页面来显示)
This workflow, while ultrasimple, will clearly demonstrate the function and usage of all the Struts 2 components, such as actions, results, and interceptors.
(这个工作流,尽管简单,将清晰的表明所有Struts2组件的作用,例如:动作、结果和拦截器)
Additionally, it’ll demonstrate the mechanics of how data flows through the framework, including the ValueStack and OGNL.
(另外,它将表明数据流如何通过框架的一种机制,包括ValueStack和OGNL)
As Struts 2 is a sophisticated framework, we’ll be limited to a high-level view.
(Struts2是一个的非常复杂的框架,我们将仅仅只是涉及视图)

HELLOWORLD USER GUIDE
Provided you’ve deployed the application to your servlet container, select the HelloWorld link from the menu we saw earlier.

(你在servlet容器上部署应用,从我们之前看到的菜单中选择HelloWorld连接)

Note that we’re starting with the XML version, not the annotated version.

(注意我们是从XML版本开始,而不是注解版本)


HELLOWORLD DETAILS

We’ll begin by looking at the architectural components used by HelloWorld.

(我们将通过使用HelloWorld来研究结构组件)

We’ve also said that many developers use this root document to include other XML documents, allowing for modularization.

(我也提到许多开发者使用根文档包括其他XML文件,以便模块化)

We’ve done this for the sample application, modularizing on the basis of chapters.

(示例程序就是这样做的,按照章来分模块)

Though off topic, we should note that the constant element ① can be used to set framework properties;

(这里需要说明常量元素①可以用来设置框架的属性)

You can also do this with property files, as we’ll see later.

(你同样能够用属性文件设置,稍后我们将会看到)

Also off topic, we should note that struts.xml is a good place to define some global actions in a default package.

(同时还需要说明,我们应该注意到struts.xml在默认包中定义一些全局动作)

Since our main menu doesn’t belong to any of our modularized mini-applications, we place it here.

(由于主菜单不属于任何模块化的迷你应用程序,所以我们把它放在这里)

Finally back on topic, we see the most important aspect of the struts.xml file, a long list of includes that pull all of our chapter-based XML documents into the declarative architecture.

(最后我们回到主题,我们看看struts.xml最重要的方面,一个长长的清单,这些清单包含将所有按照章节组织的文档引入到声明性架构)

All of these files will be pulled into this main document, in line, to create a single large XML document.

(所有这些文件会按照内联的方式被引入到主文档,以创建一个大的XML文档)

This is true for all the examples in this book, but when we print listings here we’ll remove the comments for clarity.

(本书中的所有例子都是这样的,为了清单打印清晰,我们移除了这些注释)

The only other elements here are the struts root element and the package element.

(这里仅有其它元素是struts根元素和package元素。)

The struts element is the mandatory document root of all Struts 2 XML files and the package element is an important container element for organizing your actions, results, and other component elements.

(struts元素是所有struts2 XML文件的强制性的文档根元素,package元素是一种重要的容器元素,用来组织应用程序的动作元素、结果元素和其它组件元素)

For now, the only thing we need to note about the package element is that it declares a namespace that’ll be used when the framework maps URLs to these actions.

(我们需要注意的唯一事情是关于package元素,它声明了一个当框架将URL映射到动作时会用到的命名空间)

Figure 2.6 shows how the namespace of the package is used to determine the URL that maps to our actions.

(图2-6展示了如何使用包的命名空间来决定映射到动作的URL)

The URL combines the servlet context with the package namespace and the action name.

(这个URL组合了servlet上下文、包的命名空间以及动作名称)

It merely forwards to the page that will present the user with a form to collect her name.

(它只是转到呈现给用户一个收集名字的表单的页面)

This keeps the application’s architecture consistent, prewires your workflow in anticipation of increases in complexity, and hides the real structure of your resources behind the logical namespace of the Struts 2 actions.

(这个可以让应用程序的架构保持一致性,可以预先防备工作流预计增加的复杂度,并且可以将资源的真正结构隐藏在struts2动作的逻辑命名空间之后)

While we could technically use a URL that hits the form JSP directly, a well-accepted best practice is to route these requests through actions regardless of their lack of actual processing.

(虽然从技术上来说我们能够使用URL来指向一个JSP表单,但是一个被广泛接受的最佳实践是,尽管有没有真正的处理,也使用动作来转发这些请求)

As you can see, such pass-through actions do not specify an implementation class.

(就像你看到的一样,类似这样转发用的动作不需要指定一个实现类)

They’ll automatically forward to the result they declare.

(它们将会自动指向它们定义好的结果页面)

This action points directly at the NameCollector.jsp page, which renders the form that collects the name.

(这个动作直接指向NameCollector.jsp页面,它呈现收集用户的名单)

At this point, we provide this listing only for the sake of full disclosure.

(现在,我们提供代码清单2-6的目的是为了展示其中的内容)

The HelloWorld action satisfies that contract by providing an execute() method that returns a string.

(HelloWorld 动作通过提供一个返回字符串的execute()方法来满足这个契约)

The only other important thing to note is the presence of JavaBeans properties to hold the application domain data.

(需要注意的是,这里用到了JavaBeans属性存放应用数据)

For now, recall that the action, as the MVC model component of the framework, both contains the business logic and serves as a locus of data transfer.

(现在,想一想作为框架MVC模型的组件的动作,既包含了业务逻辑,又为数据传输提供了场所)

Though there are other ways the action can hold the data, one common way is using JavaBeans properties.

(虽然动作有其他的方式来存放数据,但一种通用的方式是使用JavaBeans属性来存放)

The framework will set incoming data onto these properties when preparing the action for execution.

(当准备执行动作的时候,框架将会把访问的数据先存放到属性上面去)

First, let’s clear up some potential confusion regarding the location of data in the framework.

(首先,让我们说明一下数据在框架中的位置这个概念)

This is great because it allows convenient access to data from the action’s execute() method.

(因为动作的execute()方法可以很方便的访问数据)

So that the rest of the framework can access the data, the action object itself is placed on the ValueStack.

(为了框架的其它部分访问数据,这个动作对象本身也被放在ValueStack中)

The mechanics of the ValueStack are such that all properties of the action will then be exposed as toplevel properties of the ValueStack itself and, thus, accessible via OGNL.

(ValueStack机制就像所有动作属性一样,都会被作为ValueStack的第一级属性公开出来,这样可以通过OGNL来访问)

As figure 2.7 shows, the action holds the data, giving its own Java code convenient access.

(图2-7显示,动作用来装载数据,是为了动作组件的Java代码方便访问这些数据)

At the same time, the framework makes the properties of the action available on the ValueStack so that other regions of the framework can access the data as well.

(与此同时,框架在ValueStack上提供了这些动作的属性,以至于框架的其它区域能很好的访问数据)

In terms of our HelloWorld application, the two most important places this occurs are on the incoming form and the outgoing result page.

(在我们HelloWorld应用上,两个最重要地方发生在来自表单的数据和去向结果的页面)

In the case of the incoming request, the form field name attribute is interpreted as an OGNL expression.

(在请求的例子中,表单字段的name属性被当做OGNL表达式解析)

The expression is used to target a property on the ValueStack; in this case, the name property from our action.

(这个表达式用来指向ValueStack上的属性,此时是动作的name属性。)

The value from the form field is automatically moved onto that property by the framework.

(来自表单的值通过框架自动移动到对应的属性上)

On the other end, the result JSP pulls data off the customGreeting property by likewise using an OGNL expression, inside a tag, to reference a property on the ValueStack.

(在另一个端,JSP也需要在一个标签内使用指向ValueStack上一个属性的OGNL表达式取出属性customGreeting上的数据)

Obviously, this complicated process needs more than a quick sketch.We’ll cover it fully, particularly in chapters 5 and 6.

(很显然,这个复杂的过程需要更清晰的描述。我们会在5和6章特别的提到)


HelloWorld using annotations

As we’ve said, the annotations are placed directly in the source code of the actions.

(就像我们说的,注解直接使用在动作的源码上)

If we tell the framework where we keep our action classes, it will automatically scan them for annotations.

(如果我们告诉框架,我们的动作类在哪儿,框架将为了注解自动扫描他们)

The location of our actions is specified in an initialization parameter to the Struts 2 FilterDispatcher, defined in the web.xml deployment descriptor for the application.

(动作的位置在Struts2拦截器的初始化参数上设置,在应用程序的web.xml部署描述上定义)

But just telling the system where our action classes are isn’t enough. In addition, we must somehow identify which classes are actions.

( 但是只告诉框架动作类在哪里是不够的。另外,我们必须识别哪个类是动作类)

To mark our classes as actions, we either make our actions implement the com.opensymphony.xwork2.Action interface or use a naming convention where our class names end with the word Action.

(标记动作类,我们可以让我们的动作实现com.opensymphony.xwork2.Action接口或使用命令约定,也就是所有动作类的命名以Action结尾)

Now that the framework knows how to find our annotations, let’s see how they actually work.

(现在框架知道如何查找注解,让我们看看它们如何精确的工作)

As we discuss annotations, we’ll refer back to their counterparts in the XML version of HelloWorld.

(当我们讨论注解时,我们将会重新提起XML版本的HelloWorld应用程序中的对应内容)

The most notable thing is that several of the elements disappear entirely.

(最值得关注的事情是几个元素完全消失了)

Most importantly, the framework assumes that our namespace for this package will be derived from the Java package namespace of the action class.

(最重要的是,框架假定包元素的命名空间从动作类java包的命名空间而来)

The former, probably more correctly labeled configuration, involves tweaking or tuning the behavior and performance of the framework.

(对框架自身的配置,可能更适合称为配置,它包含了对框架的性能和行为的调节和调整)

The latter plays a much more central role in the development of our web applications;

(在我们web应用开发中,对应用程序框架的配置扮演一个非常核心的角色)

While annotations are considered by many to be more elegant than XML, we’ve opted to use XML because of its educational convenience.

(虽然很多开发人员认为java注解比XML更加优雅,但是为了便于说明,我们选择使用XML)

With the high-level overviews, architectural glosses, and the obligatory HelloWorld out of the way, we’ve officially completed the introductory portion of the book.

(在进行了宏观介绍及讲述了架构和必要的HelloWorld应用后,我们正式的完成了本书介绍部分的学习)


summary  (我们来看看HelloWorld运营机制

1、示例目录结构

First, all of the top-level directories, except WEB-INF, are in the document root of the web application.

(首先,所有的第一层应用目录,除了WEB-INF之外,都在web应用程序的文档根目录中)

One important thing to note about the document root is that these resources can potentially be served as static resources by the servlet container.

(一件非常重要的事情需要注意:文档根目录的这些资源能够被servlet容器作为静态资源使用)

Because of this, the document root is not considered a secure place for sensitive material.

(就因为这个,文档根目录对于一些敏感信息的来说不是安全场所)

if you want to see the absolute minimum set of JARs, you should check out HelloWorld.war, referenced in an earlier sidebar.

(如果你想查看Struts 2必需的最小的JAR集合,可以参看之前讲解的HelloWorld.war文件)

This file, formally known as the deployment descriptor, contains definitions of all of the servlets, servlet filters, and other Servlet API components contained in this web application.

(WEN-INF文件的正式名称是部署描述符,它包含了web应用所有的servlet、servlet过滤器和其他servlet API组件的定义)

For a Struts 2 application, the most important elements in this deployment descriptor are the filter and filter-mapping elements that set up the Struts 2 Filter-Dispatcher.

(对于Struts 2应用来说,在部署描述符中最重要的元素是filter和filter-mapping元素,它们组成了Struts 2的Filter-Dispatcher)

This servlet filter is basically the Struts 2 framework.

(这个servlet过滤器是Struts 2框架的基础)

This filter will examine all the incoming requests looking for requests that target Struts 2 actions.

(它检查所有传入的请求,查找指向Struts 2动作的请求)

The other important thing about the configuration of the Filter- Dispatcher is the initialization parameter that we pass in.

(另外一个关于Filter-Dispatcher重要的配置是我们传入的初始化参数)

The actionPackages parameter is necessary if you’re going to use annotations in your application.

(如果你在你的应用中使用注解,那么这个actionPackages参数就会被用到)

In fact, since Struts 2 uses a servlet filter rather than a servlet, many Struts 2 applications won’t have any servlets in them—unless you count compiled JSPs. 

(实际上,由于Struts 2使用Servlet过滤器而不是Servlet,所以很多Struts 2应用程序不包含任何Servlet -- 除非JSP编译得到的Servlet也算在内。)

Since it’s not uncommon to integrate other servlets with the framework, we’ve included another servlet, as seen in listing 2.3, in our web application.

(由于整合框架和其它的servlet并不罕见,所以我们在应用程序包含了另外一个servlet)

Also off topic, we should note that struts.xml is a good place to define some global actions in a default package.

(同时还需要说明struts.xml文件是在默认包内定义全局动作的好地方)

Since our main menu doesn’t belong to any of our modularized mini-applications, we place it here.

(由于主菜单不属于任何模块化迷你应用程序,所以我们把它放在这里)

The struts element is the mandatory document root of all Struts 2 XML files and the package element is an important container element for organizing your actions, results, and other component elements.

(struts元素是所有Struts 2 XML文件的强制性的文档根元素,package元素是一个重要的容器元素,用来组织应用程序的动作、结果、和其它组件元素)

For now, the only thing we need to note about the package element is that it declares a namespace that’ll be used when the framework maps URLs to these actions.

(现在,对package元素,唯一需要注意的地方是它声明了一个当框架将URL映射到动作时会用到的命名空间)









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值