📄 building_view.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>The Struts User's Guide - Building View Components</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="Craig R. McClanahan" name="author" />
<meta content="Mike Schachter" name="author" />
<meta content="Ted Husted" name="author" />
<meta content="Martin Cooper" name="author" />
<meta content="Ed Burns" name="author" />
<meta content="James DeVries" name="author" />
<meta content="David Graham" name="author" />
<link href="../struts.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="heading">
<a href="http://apache.org/">
<img id="asf_logo_wide" alt="The Apache Project" src="../images/asf_logo_wide.gif" />
</a>
<a href="http://struts.apache.org/">
<img id="struts-logo" alt="Struts Framework" src="../images/struts.gif" />
</a>
</div>
<!--end heading-->
<div id="content">
<div id="menu">
<p>User Guide</p>
<ul>
<li>
<a href="index.html">Table of Contents</a>
</li>
<li>
<a href="preface.html">Preface</a>
</li>
<li>
<a href="introduction.html">Introduction</a>
</li>
<li>
<a href="building_model.html">Model Components</a>
</li>
<li>
<a href="building_view.html">View Components</a>
</li>
<li>
<a href="building_controller.html">Controller Components</a>
</li>
<li>
<a href="configuration.html">Configuration</a>
</li>
<li>
<a href="release-notes.html">Release Notes</a>
</li>
<li>
<a href="installation.html">Installation</a>
</li>
</ul>
<p>Developer Guides</p>
<ul>
<li>
<a href="dev_bean.html">Bean Tags</a>
</li>
<li>
<a href="dev_html.html">HTML Tags</a>
</li>
<li>
<a href="dev_logic.html">Logic Tags</a>
</li>
<li>
<a href="dev_nested.html">Nested Tags</a>
</li>
<li>
<a href="dev_tiles.html">Tiles Tags</a>
</li>
<li>
<a href="dev_util.html">Utilities</a>
</li>
<li>
<a href="dev_validator.html">Validator</a>
</li>
</ul>
<p>Quick Links</p>
<ul>
<li>
<a href="../index.html">Welcome</a>
</li>
<li>
<a href="index.html">User and Developer Guides *</a>
</li>
<li>
<a href="../faqs/index.html">FAQs and HowTos</a>
</li>
</ul>
<div class="authors">
<p>
<strong>Contributors</strong>
</p>
<ul>
<li>Craig R. McClanahan</li>
<li>Mike Schachter</li>
<li>Ted Husted</li>
<li>Martin Cooper</li>
<li>Ed Burns</li>
<li>James DeVries</li>
<li>David Graham</li>
</ul>
</div>
</div>
<!--end menu-->
<div id="main">
<h1 id="building_view">3. Building View Components</h1>
<h2 id="overview">3.1 Overview</h2>
<div class="indent">
<p>
This chapter focuses on the task of building the <em>View</em> components
for use with the Struts framework.
Many applications rely on JavaServer Pages (JSP) technology to create the
presentation layer.
The Struts distribution includes a comprehensive JSP tag library that
provides support for building internationalized applications, as well as
for interacting with input forms.
Several other topics related to the View components are briefly discussed.
</p>
</div>
<h2 id="i18n">3.2 Internationalized Messages</h2>
<div class="indent">
<p>
A few years ago, application developers could count on having to support
only residents of their own country, who are used to only one (or
sometimes two) languages, and one way to represent numeric quantities like
dates, numbers, and monetary values.
However, the explosion of application development based on web
technologies, as well as the deployment of such applications on the
Internet and other broadly accessible networks, have rendered national
boundaries invisible in many cases.
This has translated (if you will pardon the pun) into a need for
applications to support <em>internationalization</em> (often called "i18n"
because 18 is the number of letters in between the "i" and the "n") and
<em>localization</em>.
</p>
<p>
Struts builds upon the standard classes available on the Java platform to
build internationalized and localized applications.
The key concepts to become familiar with are:
</p>
<ul>
<li>
<a href="http://java.sun.com/j2se/1.4.1/docs/api/java/util/Locale.html">
<strong>Locale</strong>
</a> - The fundamental Java class that supports
internationalization is <code>Locale</code>.
Each <code>Locale</code> represents a particular choice of country and
language (plus an optional language variant), and also a set of
formatting assumptions for things like numbers and dates.
</li>
<li>
<a href="http://java.sun.com/j2se/1.4.1/docs/api/java/util/ResourceBundle.html">
<strong>ResourceBundle</strong>
</a> - The <code>java.util.ResourceBundle</code>
class provides the fundamental tools for supporting messages in
multiple languages.
See the Javadocs for the <code>ResourceBundle</code> class, and the
information on Internationalization in the documentation bundle for your
JDK release, for more information.
</li>
<li>
<a href="http://java.sun.com/j2se/1.4.1/docs/api/java/util/PropertyResourceBundle.html">
<strong>PropertyResourceBundle</strong>
</a> - One of the standard
implementations of <code>ResourceBundle</code> allows you to define
resources using the same "name=value" syntax used to initialize
properties files.
This is very convenient for preparing resource bundles with messages
that are used in a web application, because these messages are
generally text oriented.
</li>
<li>
<a href="http://java.sun.com/j2se/1.4.1/docs/api/java/text/MessageFormat.html">
<strong>MessageFormat</strong>
</a> - The <code>java.text.MessageFormat</code>
class allows you to replace portions of a message string (in this
case, one retrieved from a resource bundle) with arguments specified
at run time.
This is useful in cases where you are creating a sentence, but the
words would appear in a different order in different languages.
The placeholder string <code>{0}</code> in the message is replaced by
the first runtime argument, <code>{1}</code> is replaced by the
second argument, and so on.
</li>
<li>
<a href="../api/org/apache/struts/util/MessageResources.html">
<strong>MessageResources</strong>
</a> - The Struts class
<code>org.apache.struts.util.MessageResources</code> lets you treat
a set of resource bundles like a database, and allows you to request
a particular message string for a particular Locale (normally one
associated with the current user) instead of for the default Locale
the server itself is running in.
</li>
</ul>
<p>
Please note that the i18n support in a framework like Struts is limited to
the <strong>presentation</strong> of internationalized text and images to the user.
Support for Locale specific <strong>input methods</strong> (used with languages
such as Japanese, Chinese, and Korean) is left up to the client device,
whichis usually a web browser.
</p>
<p>
For an internationalized application, follow the steps described in
the Internationalization document in the JDK documentation bundle for your
platform to create a properties file containing the messages for each
language.
An example will illustrate this further:
</p>
<p>
Assume that your source code is created in package
<code>com.mycompany.mypackage</code>, so it is stored in a directory
(relative to your source directory) named
<code>com/mycompany/mypackage</code>.
To create a resource bundle called
<code>com.mycompany.mypackage.MyApplication</code>, you would create the
following files in the <code>com/mycompany/mypackage</code> directory:
</p>
<ul>
<li>
<strong>MyApplication.properties</strong> - Contains the messages in the default
language for your server.
If your default language is English, you might have an entry like
this: <code>prompt.hello=Hello</code>
</li>
<li>
<strong>MyApplication_xx.properties</strong> - Contains the same messages in the
language whose ISO language code is "xx" (See the
<a href="http://java.sun.com/j2se/1.4.1/docs/api/java/util/ResourceBundle.html">
ResourceBundle Javadoc</a> page for a link to the current list).
For a French version of the message shown above, you would have this
entry: <code>prompt.hello=Bonjour</code>
You can have resource bundle files for as many languages as you need.
</li>
</ul>
<p>
When you configure the controller servlet in the web application
deployment descriptor, one of the things you will need to define in
an initialization parameter is the base name of the resource bundle
for the application.
In the case described above, it would be
<code>com.mycompany.mypackage.MyApplication</code>.
</p>
<pre>
<code>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>
com.mycompany.mypackage.MyResources
</param-value>
</init-param>
<!-- ... -->
</servlet>
</code>
</pre>
<p>
The important thing is for the resource bundle to be found on the
class path for your application.
Another approach is to store the <code>MyResources.properties</code>
file in your application's <code>classes</code> folder.
You can then simply specify "myResources" as the application value.
Just be careful it is not deleted if your build script deletes
classes as part of a "clean" target.
</p>
<p>
If it does, here is an Ant task to run when compiling your application
that copies the contents of a <code>src/conf</code>
directory to the <code>classes</code> directory:
</p>
<pre>
<code>
<!-- Copy any configuration files -->
<copy todir="classes">
<fileset dir="src/conf"/>
</copy>
</code>
</pre>
</div>
<h2 id="form_beans">3.3 Forms and FormBean Interactions</h2>
<div class="indent">
<p>
<strong>Note:</strong> While the examples given here use JSP and custom tags,
the ActionForm beans and the other Struts controller components are
View neutral.
Struts can be used with Velocity Templates, XSL, and any other
presentation technology that can be rendered via a Java servlet.
</p>
<p>
At one time or another, most web developers have built forms using
the standard capabilities of HTML, such as the <code><input></code>
tag.
Users have come to expect interactive applications to have certain
behaviors, and one of these expectations relates to error handling -- if
the user makes an error, the application should allow them to fix just
what needs to be changed -- without having to re-enter any of the rest
of the information on the current page or form.
</p>
<p>
Fulfilling this expectation is tedious and cumbersome when coding with
standard HTML and JSP pages.
For example, an input element for a <code>username</code> field might
look like this (in JSP):
</p>
<pre>
<code>
<input type="text" name="username"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -