文档库 最新最全的文档下载
当前位置:文档库 › 计算机科学与技术设计论文外文翻译

计算机科学与技术设计论文外文翻译

计算机科学与技术设计论文外文翻译
计算机科学与技术设计论文外文翻译

计算机科学与技术设计论文外文翻译

本科毕业设计(论文)外文翻译(附外文原文)

系 ( 院 ):信息科学与工程学院

课题名称:学生信息管理系统

专业(方向):计算机科学与技术(应用)

7.1 Enter ActionMappings

The Model 2 architecture (see chapter 1) encourages us to use servlets and Java- Server Pages in the same application. Under Model 2, we start by calling a servlet. The servlet handles the business logic and directs control to the appropriate page

to complete the response.

The web application deployment descriptor (web.xml) lets us map a URL pattern

to a servlet. This can be a general pattern, like *.do, or a specific path, like saveRecord.do.

Some applications implement Model 2 by mapping a servlet to each business operation. This approach works, but many applications involve dozens or hundreds of business operations. Since servlets are multithreaded, instantiating so manyservlets is not the best use of server resources. Servlets are designed to handle any

number of parallel requests. There is no performance benefit in simply creating more and more servlets.

The servlet’s primary job is to interact with the container and HTTP. Handling

a business operation is something that a servlet could delegate to another component. Struts does this by having the ActionServlet delegate the business operation

to an object. Using a servlet to receive a request and route it to a handler is known as the Front Controller pattern [Go3].

Of course, simply delegating the business operation to another component

does not solve the problem of mapping URIs [W3C, URI] to business operations. Our only way of communicating with a web browser is through HTTP requests and URIs. Arranging for a URI to trigger a business operation is an essential part of developing a web application.

Meanwhile, in practice many business operations are handled in similar ways.

Since Java is multithreaded, we could get better use of our server resources if we could use the same Action object to handle similar operations. But for this to work, we might need to pass the object a set of configuration parameters to use

with each operation.

So what’s the bottom line? To implement Model 2 in an efficient and flexible

way, we need to:

Enter ActionMappings 195

_ Route requests for our business operations to a single servlet

_ Determine which business operation is related to the request

_ Load a multithreaded helper object to handle the business operation

_ Pass the helper object the specifics of each request along with any configuration detail used by this operation

This is where ActionMappings come in.

7.1.1 The ActionMapping bean

An ActionMapping (org.apache.struts.action.ActionMapping) describes how

the framework handles each discrete business operation (or action). In Struts,

each ActionMapping is associated with a specific URI through its path property. When a request comes in, the ActionServlet uses the path property to select the corresponding ActionMapping. The set of ActionMapping objects is kept in an ActionMappings collection (org.apache.struts.action.ActionMappings). Originally, the ActionMapping object was used to extend the Action object

rather than the Action class. When used with an Action, a mapping gives a specific Action object additional responsibilities and new functionality. So, it was essentially an Action decorator [Go4]. Along the way, the ActionMapping evolved into an

object in its own right and can be used with or without an Action.

DEFINITION The intent of the decorator pattern is to attach additional responsibilities to

an object dynamically. Decorators provide a flexible alternative to subclassing

for extending functionality [Go4].

The ActionMappings are usually created through the Struts configuration file.

For more about this file, see chapter 4.

7.1.2 The ActionMappings catalog

The ActionMappings catalog the business logic available to a Struts application. When a request comes in, the servlet finds its entry in the ActionMappings catalog and pulls the corresponding bean.

The ActionServlet uses the ActionMapping bean to decide what to do next. It

may need to forward control off to another resource. Or it may need to populate and validate an ActionForm bean. At some point, it may have to pass control to an Action object, and when the Action returns, it may have to look up an Action- Forward associated with this mapping.

196 CHAPTER 7

Designing with ActionMappings

The ActionMapping works like a routing slip for the servlet. Depending on

how the mapping is filled out, the request could go just about anywhere.

The ActionMappings represent the core design of a Struts application. If you

want to figure out how a Struts application works, start with the ActionMappings. If you want to figure out how to write a new Struts application, start with the Action- Mappings. The mappings are at the absolute center of every Struts application.

In this chapter, we take a close look at the ActionMapping properties and

explore how they help you design the flow of a Struts application.

1.0 vs 1.1 In Struts 1.1, ActionMapping subclasses ActionConfig (org.apache. struts.config.ActionConfig) and adds API methods required for backward compatibility. ActionMapping is not deprecated, and how the

hierarchy will be handled in future releases has not been determined.

For now, we refer to the ActionMapping class, but you should note that

in Struts 1.1 all of the action properties are actually defined by the ActionConfig super class. The ActionMapping class otherwise works the

same way in both versions.

7.2 ActionMapping properties

Table 7.1 describes the base ActionMapping properties. As with other configuration components, developers may extend ActionMapping to provide additional

properties.

Table 7.1 The base ActionMapping properties

Property Description

path The URI path from the request used to select this mapping. (API command)

forward The context-relative path of the resource that should serve this request via a forward.

Exactly one of the forward, include, or type properties must be specified. or

include The context-relative path of the resource that should serve this request via an

include. Exactly one of the forward, include, or type properties must be specified.

or

type Optionally specifies a subclass of

org.apache.struts.action.ActionMapping

that should be used when instantiating this mapping.

className The fully qualified name of the Action class used by this mapping. Since

Struts 1.1

ActionMapping properties 197

In the sections that follow, we take a look at each of these properties.

7.2.1 The path property

The ActionMapping URI, or path, will look to the user like just another file on

the web server. But it does not represent a file. It is a virtual reference to our ActionMapping.

Because it is exposed to other systems, the path is not really a logical name, like those we use with ActionForward. The path can include slashes and an extension—as if it referred to a file system—but they are all just part of a single name.

The ActionMappings themselves are a “flat” namespace with no type of internal hierarchy whatsoever. They just happen to use the same characters that we are

used to seeing in hierarchical file systems.

name The name of the form bean, if any, associated with this action. This is not the class

name. It is the logical name used in the form bean configuration.

roles The list of security roles that may access this mapping.

scope The identifier of the scope (request or session) within which the form bean, if any,

associated with this mapping will be created.

validate Set to true if the validate method of the form bean (if any) associated with this

mapping should be called.

input Context-relative path of the input form to which control should be returned if a validation

error is encountered. This can be any URI: HTML, JSP, VM, or another Action- Mapping.

parameter General-purpose configuration parameter that can be used to pass extra information

to the Action selected by this ActionMapping.

attribute Name of the request-scope or session-scope attribute under which our form bean is

accessed, if it is other than the bean's specified name.

prefix Prefix used to match request parameter names to form bean property names, if any.

suffix Suffix used to match request parameter names when populating the properties of

our ActionForm bean, if any.

unknown Can be set to true if this mapping should be configured as the default for this application

(to handle all requests not handled by another mapping). Only one mapping can be defined as the default unknown mapping within an application. forwards(s) Block of ActionForwards for this mapping to use, if any. exception(s) Block of ExceptionHandlers for this mapping to use, if any. Table 7.1 The base ActionMapping properties (continued)

Property Description

Since

Struts 1.1

Since

Struts 1.1

198 CHAPTER 7

Designing with ActionMappings

Of course, it can still be useful to treat your ActionMappings as if they were

part of a hierarchy and group related commands under the same "folder." The

only restriction is that the names must match whatever pattern is used in the application’s deployment description (web.xml) for the ActionServlet. This is usually either /do/* or *.do, but any similar pattern can be used.

If you are working in a team environment, different team members can be

given different ActionMapping namespaces to use. Some people may be working

with the /customer ActionMappings, others may be working with the /vendor ActionMappings. This may also relate to the Java package hierarchy the team is using. Since the ActionMapping URIs are logical constructs, they can be organized

in any way that suits your project.

With Struts 1.1, these types of namespaces can be promoted to application modules. Each team can work independently on its own module, with its own set

of configuration files and presentation pages. Configuring your application to use multiple modules is covered in chapter 4.

DEFINITION The web runs on URIs, and most URIs map to physical files. If you want to

change the resource, you change the corresponding file. Some URIs, like

Struts actions, are virtual references. They do not have a corresponding

file but are handled by a programming component. To change the resource,

we change how the component is programmed. But since the

path is a URI and interacts with other systems outside our control, the

path is not a true logical reference—the name of an ActionForward, for

instance. We can change the name of an ActionForward without consulting

other systems. It’s an internal, logical reference. If we change the

path to an ActionMapping, we might need to update other systems that

refer to the ActionMapping through its public URI.

7.2.2 The forward property

When the forward property is specified, the servlet will not pass the request to an Action class but will make a call to RequestDispatcher.forward. Since the operation

does not use an Action class, it can be used to integrate Struts with other

resources and to prototype systems. The forward, include, and type properties are mutually exclusive. (See chapter 6 for more information.)

7.2.3 The include property

When the include property is specified, the servlet will not pass the request to an Action class but will make a call to RequestDispatcher.include. The operation ActionMapping properties 199

does not use an Action class and can be used to integrate Struts with other components.

The forward, include, and type properties are mutually exclusive. (See

chapter 6 for more information.)

7.2.4 The type property

Most mappings will specify an Action class type rather than a forward or include.

An Action class may be used by more than one mapping. The mappings may specify form beans, parameters, forwards, or exceptions. The forward, include, and

type properties are mutually exclusive.

7.2.5 The className property

When specified, className is the fully qualified Java classname of the ActionMapping

subclass that should be used for this object. This allows you to use your own ActionMapping subclass with specialized methods and properties. See also section 7.4.

7.2.6 The name property

This property specifies the logical name for the form bean, as given in the formbean segment of the Struts configuration file. By default, this is also the name to

be used when placing the form bean in the request or session context. Use the attribute property of this class to specify a different attribute key.

7.2.7 The roles property

This property is a comma-delimited list of the security role names that are allowed access to this ActionMapping object. By default, the same system that is used with standard container-based security is applied to the list of roles given here. This means you can use action-based security in lieu of specifying URL patterns in the deployment descriptor, or you can use both together.

The security check is handled by the processRoles method of the Request- Processor (org.apache.struts.action.RequestProcessor). By subclassing RequestProcessor, you can also use the roles property with application-based security. See chapter 9 for more about subclassing RequestProcessor.

7.2.8 The scope property

The ActionForm bean can be stored in the current request or in the session scope (where it will be available to additional requests). While most developers use

request scope for the ActionForm, the framework default is session scope. To

make request the default, see section 7.4.

Since

Struts 1.1

Since

Struts 1.1

200 CHAPTER 7

Designing with ActionMappings

7.2.9 The validate property

An important step in the lifecycle of an ActionForm is to validate its data before offering it to the business layer. When the validate property for a mapping is true, the ActionServlet will call the ActionForm’s validate method. If validate returns

false, the request is forwarded to the resource given by the input property. Often, developers will create a pair of mappings for each data entry form. One mapping will have validate set to false, so you can create an empty form. The other has validate set to true and is used to submit the completed form.

NOTE Whether or not the ActionForm validate method is called does not relate

to the ActionServlet’s validating property. That switch controls

how the Struts configuration file is processed.

7.2.10 The input property

When validate is set to true, it is important that a valid path for input be provided. This is where control will pass should the ActionForm validate method

return false. Often, this is the address for a presentation page. Sometimes it will

be another Action path (with validate set to false) that is required to generate data objects needed by the page.

NOTE The input path often leads back to the page that submitted the request.

While it seems natural for the framework to return the request to where

it originated, this is not a simple task in a web application. A request is often

passed from component to component before a response is sent back

to the browser. The browser only knows the path it used to retrieve the

input page, which may or may not also be the correct path to use for the

input property. While it may be possible to try and generate a default input

page based on the HTTP referrer attribute, the Struts designers

deemed that approach unreliable.

inputForward

In Struts 1.0, the ActionMapping input property is always a literal URI. In

Struts 1.1, it may optionally be the name of an ActionForward instead. The ActionForward is retrieved and its path property is used as the input property. This can be a global or local ActionForward.

To use ActionForwards here instead of literal paths, set the inputForward attribute on the element for this module to true:

Since

Struts 1.1

ActionMapping properties 201

For more about configuring Struts, see chapter 4. For more about ActionForwards, see chapter 6.

7.2.11 The parameter property

The generic parameter property allows Actions to be configured at runtime. Several of the standard Struts Actions make use of this property, and the standard

Scaffold Actions often use it, too. The parameter property may contain a URI, the name of a method, the name of a class, or any other bit of information an Action

may need at runtime. This flexibility allows some Actions to do double and triple duty, slashing the number of distinct Action classes an application needs on hand. Within an Action class, the parameter property is retrieved from the mapping passed to perform:

parameter = mapping.getParameter();

Multiple parameters

While multiple parameters are not supported by the standard ActionMappings class, there are some easy ways to implement this, including using HttpUtils, a StringTokenizer, or a Properties file (java.util.Properties).

HttpUtils. Although deprecated as of the Servlet API 2.3 specification, the

HttpUtils package (javax.servlet.http.HttpUtils) provides a static method

that parses any string as if it were a query string and returns a Hashtable (java.util.Hashtable):

Hashtable parameters = parseQueryString(parameter);

The parameter property for your mapping then becomes just another query string, because you might use it elsewhere in the Struts configuration. stringTokenizer. Another simple approach is to delimit the parameters using the token of your choice—such as a comma, colon, or semicolon—and use the StringTokenizer to read them back:

StringTokenizer incoming =

new StringTokenizer(mapping.getParameter(),";");

int i = 0;

String[] parameters = new String[incoming.countTokens()]; while (incoming.hasMoreTokens()) {

parameters[i++] = incoming.nextToken().trim();

}

202 CHAPTER 7

Designing with ActionMappings

Properties file. While slightly more complicated than the others, another popular approach to providing multiple parameters to an ActionMapping is with a standard Properties files (java.util.Properties). Depending on your needs, the Properties file could be stored in an absolute location in your file system or anywhere on your application’s CLASSPATH.

The Commons Scaffold package [ASF, Commons] provides a ResourceUtils package (https://www.wendangku.net/doc/c2756286.html,mons.scaffold.util.ResourceUtils) with methods for

loading a Properties file from an absolute location or from your application’s CLASSPATH.

7.2.12 The attribute property

From time to time, you may need to store two copies of the same ActionForm in

the same context at the same time. This most often happens when ActionForms

are being stored in the session context as part of a workflow. To keep their names from conflicting, you can use the attribute property to give one ActionForm bean a different name.

An alternative approach is to define another ActionForm bean in the configuration, using the same type but under a different name.

7.2.13 The prefix and suffix properties

Like attribute, the prefix and suffix properties can be used to help avoid naming conflicts in your application. When specified, these switches enable a

prefix or suffix for the property name, forming an alias when it is populated

from the request.

If the prefix this was specified, then

thisName=McClanahan

becomes equivalent to

name=McClanahan

for the purpose of populating the ActionForm. Either or both parameters would call getName("McClanahan");

This does not affect how the properties are written by the tag extensions. It affects how the autopopulation mechanism perceives them in the request.

Nested components 203

7.2.14 The unknown ActionMapping

While surfing the Web, most of us have encountered the dreaded 404— page not found message. Most web servers provide some special features for processing requests for unknown pages, so webmasters can steer users in the right direction. Struts offers a similar service for ActionMapping 404s—the unknown ActionMapping. In the Struts configuration file, you can specify one ActionMapping to

receive any requests for an ActionMapping that would not otherwise be matched:

name="/debug"

forward="/pages/debug.jsp"/>

When this option is not set, a request for an ActionMapping that cannot be

matched throws

400 Invalid path /notHere was requested

Note that by a request for an ActionMapping, we mean a URI that matches the prefix or suffix specified for the servlet (usually /do/* or *.do). Requests for other URI patterns, good or bad, will be handled by other servlets or by the container:

/do/notHere (goes to the unknown ActionMapping)

/notHere.txt (goes to the container)

7.3 Nested components

The ActionMapping properties are helpful when it comes to getting an Action to

run a business operation. But they tell only part of the story. There is still much to

do when the Action returns.

An Action may have more than one outcome. We may need to register several ActionForwards so that the Action can take its pick.

7.3.1 Local forwards

In the normal course, an ActionMapping is used to select an Action object to handle the request. The Action returns an ActionForward that indicates which page

should complete the response.

The reason we use ActionForwards is that, in practice, presentation pages are

either often reused or often changed, or both. In either case, it is good practice to encapsulate the page’s location behind a logical name, like “success” or “failure.”

The ActionForward object lets us assign a logical name to any given URI.

204 CHAPTER 7

Designing with ActionMappings

Of course, logical concepts like success or failure are often relative. What represents success to one Action may represent failure to another. Each Action-

Mapping can have its own set of local ActionForwards. When the Action asks for a forward (by name), the local set is checked before trying the global forwards. See chapter 6 for more about ActionForwards.

Local forwards are usually specified in the Struts configuration file. See chapter

4 for details.

7.3.2 Local exceptions

Most often, an application’s exception handlers (org.apache.struts.action. ExceptionHandler) can be declared globally. However, if a given ActionMapping needs to handle an exception differently, it can have its own set of local exception handlers that are checked before the global set.

Local exceptions are usually specified in the Struts configuration file. See

chapter 4 for details.

7.4 Rolling your own ActionMapping

While ActionMapping provides an impressive array of properties, developers may also provide their own subclass with additional properties or methods. In

Struts 1.0, this is configured in the deployment descriptor (web.xml) for the ActionServlet:

mapping

app.MyActionMapping

In Struts 1.1, this is configured in the Struts configuration file as an attribute to

the element:

Individual mappings may also be set to use another type through the className attribute:

For more about configuring Struts, see chapter 4.

Since

Struts 1.1

Summary 205

The framework provides two base ActionMapping classes, shown in table 7.2. They can be selected as the default or used as a base for your own subclasses.

The framework default is SessionActionMapping, so scope defaults to session. Subclasses that provide new properties may set them in the Struts configuration using a standard mechanism:

Using this standard mechanism helps developers avoid subclassing the Action- Servlet just to recognize the new properties when it digests the configuration file. This is actually a feature of the Digester that Struts simply inherits.

7.5 Summary

Sun’s Model 2 architecture teaches that servlets and JavaServer Pages should be used together in the same application. The servlets can handle flow control and data acquisition, and the JavaServer Pages can handle the HTML.

Struts takes this one step further and delegates much of the flow control and

data acquisition to Action objects. The application then needs only a single servlet to act as a traffic cop. All the real work is parceled out to the Actions and the Struts configuration objects.

Like servlets, Actions are efficient, multithreaded singletons. A single Action object can be handling any number of requests at the same time, optimizing your server’s resources.

To get the most use out of your Actions, the ActionMapping object is used as a decorator for the Action object. It gives the Action a URI, or several URIs, and a way to pass different configuration settings to an Action depending on which URI is called.

In this chapter, we took a close look at the ActionMapping properties and explained each property’s role in the scheme of things. We also looked at extending the standard ActionMapping object with custom properties—just in case your scheme needs even more things.

Table 7.2 The default ActionMapping classes

ActionMapping Description

org.apache.struts.action.SessionActionMapping Defaults the scope property to session

org.apache.struts.action.RequestActionMapping Defaults the scope property to request

206 CHAPTER 7

Designing with ActionMappings

In chapter 8, the real fun begins. The configuration objects covered so far are

mainly a support system. They help the controller match an incoming request

with a server-side operation. Now that we have the supporting players, l et’s meet the Struts diva: the Action object.

7.1 进入ActionMapping

Model 2 架构(第1章)鼓励在同一个应用中使用servlet和JSP页面。在Model 2下,我们

从调用一个servlet开始。

Servlet 处理业务逻辑并将控制转到相应的页面来完成响应。web应用部署描述符(web.xml) 让我们可以映射一个URL模板给一个servlet。这可以是一个常用的模板格式,象

*.do, 或者特别的路径, 象saveRecord.do。某些应用通过给每个业务操作映射一个Servlet来实

现Model 2。这种方法可以工作,但许多应用也涉及到大量的业务操作。因为servlet 是多线

程的,实例化这么多servlet 显然不是使用服务器资源的最好方式。Servlet设计来可以处理大

量的并行请求。简单的创建很多servlet并没有什么性能优势。

Servlet的主要工作是和容器和HTTP进行交互。处理业务操作是servlet代表其它组件

干的

事情。Struts 通过使ActionServlet 代表其他对象的业务操作来做这些事情。用servlet 接受

请求并路由到一个处理器(称为前端控制器模式[Go3])。

当然, 简单的代表其他对象的业务操作并没有解决映射URI [W3C, URI] 到业务操作的问

题。我们和Web浏览器进行通信能使用的唯一途径是HTTP 请求和URI。如何组织URI 来触

发业务操作是开发web 应用的关键部分。

同时,在实际应用中,许多业务操作都以相似的方式来进行处理。因为Java 是多线程

的,如果我们使用同一个Action 对象来处理相似的操作,就能更好的利用服务器资源。但

是要让它能工作,我们可能需要向对象传递一些配置参数,以便和每个操作一起使用。所以,底线是什么?为以一种灵活和有效的方式实现Model 2,我们需要

路由对业务操作的请求到一个servlet

决定哪个业务操作和请求相关

装入多线程的helper 对象来处理业务操作

将每个请求特定的配置细节传递给helper对象。

这样,ActionMapping出场了!

7.1.1 ActionMapping bean

ActionMapping (org.apache.struts.action.ActionMapping) 描述了框架

是如何处

理每一离散的业务操作(或action)。在Struts中,每个ActionMapping 通过其path 属性和

一个特定的URI 相关。

当一个请求到来,ActionServlet 使用path 属性来选择对应的ActionMapping。一系列

ActionMapping 对象被放在一个ActionMappings 集合之中

(org.apache.struts.action.ActionMappings)。原本, ActionMapping 对象用来扩展

Action 对象而不是Action 类。当和Action使用时,mapping 给了一个特定的Action

对象一

些额外的职责和新的功能。所以本质上来说,Action 是一个装饰器(decorator [Go4])。通

过这种方式,ActionMapping 以自己的方式发展为一个对象,可以和Action一起使用,也

可以不。

Struts In Action

DEFINITION

装饰器模式(decorator pattern)的意图是为一个对象动态的附加上额外的职责和功能。Decorator

在扩展功能时提供了一个除子类化外的选择。[Go4].

ActionMapping通常通过Struts 配置文件创建。更多信息,参见第4章。

7.1.2 ActionMapping 目录

ActionMapping将对Struts 应用有效的业务逻辑进行编目。当一个请求到达时,servlet 在

ActionMapping目录中查找条目,来调用相应的bean。ActionServlet 使用ActionMapping bean

来决定接下来该做什么。它也许需要将控制转发到其他资源。或者也许它需要组装并且校验

一个ActionForm bean。某些时候,它也许会将控制传递给一个Action 对象,并且当Action 返

回时,它可能会查找和这个mapping相关的ActionForward。

ActionMapping工作起来就像是servlet的一个路由联络。取决于mapping 如何被填充,可

能被传递到上述的任何地方。ActionMapping表达了Struts 应用的核心设计。如果你想知道

一个Struts 应用是如何工作的,可以从ActionMapping开始着手。如果你想知道如何编写一

个新的Struts 应用,也请从ActionMapping开始。Mapping处于每个Struts 应用的绝对核心。

在本章,我们将详细讨论ActionMapping 属性,并展示他们如何有助于设计Struts 应用的流

程。

1.0 vs 1.1

在Struts 1.1中, ActionMapping 子类化了ActionConfig

(org.apache.struts.config.ActionConfig) 类,并因向后兼容添加了API 方法。ActionMapping 已经不推荐,但后继版本中类的层次还没有决定。

当前,我们引用ActionMapping 类,但你应该注意到在Struts 1.1中,所有的action 属性实际上

都在ActionConfig超类中有定义。ActionMapping类可以工作在两个版本下。

7.2 ActionMapping 属性

表7.1 描述了基本的属性。当和其他配置组件一起,开发人员可以扩展ActionMapping to 提

供附加的属性。

属性说明

path

来自于请求的URI路径,用来选择该mapping。(API command)

forward

上下文相关的资源路径,通过一个forward服务这个请求。

实际上是forward, include, type 属性的一个,必须标明。

include

上下文相关的资源路径,通过一个include服务这个请求。

实际上是forward, include, type 属性的一个,必须标明。

type 可选,表明一个org.apache.struts.action.ActionMapping的子类名称,在实例化这个mapping

Struts In Action

时使用。

classname 该Mapping使用的Action 类的全限定名称

name

与该Mapping相关的form bean的名称,如果有。这不是类名称。而是在form bean 配置中

使用的逻辑名称。

相关文档