📄 building_view.html
字号:
value="<%= loginBean.getUsername() >"/>
</code>
</pre>
<p>
which is difficult to type correctly, confuses HTML developers who are
not knowledgeable about programming concepts, and can cause problems with
HTML editors.
Instead, Struts provides a comprehensive facility for building forms,
based on the Custom Tag Library facility of JSP 1.1.
The case above would be rendered like this using Struts:
</p>
<pre>
<code>
<html:text property="username"/>;
</code>
</pre>
<p>
with no need to explicitly refer to the JavaBean from which the initial
value is retrieved. That is handled automatically by the JSP tag, using
facilities provided by the framework.
</p>
<p>
HTML forms are sometimes used to upload other files.
Most browsers support this through a <input type="file"> element,
that generates a file browse button, but it's up to the developer to
handle the incoming files.
Struts handles these "multipart" forms in a way identical to building
normal forms.
</p>
<p>
For an example of using Struts to create a simple login form,
see the "<a href="../faqs/actionForm.html">
Buiding an ActionForm Howto</a>".
</p>
</div>
<h2 id="indexed">3.3.1 Indexed & Mapped Properties</h2>
<div class="indent">
<p>
Property references in JSP pages using the Struts framework can reference
Java Bean properties as described in the JavaBeans specification.
Most of these references refer to "scalar" bean properties, referring to
primitive or single Object properties.
However, Struts, along with the Jakarta Commons Beanutils library, allow
you to use property references which refer to individual items in an array,
collection, or map, which are represented by bean methods using
well-defined naming and signature schemes.
</p>
<p>
Documentation on the Beanutils package can be found at
<a href="http://jakarta.apache.org/commons/beanutils/api/index.html">
http://jakarta.apache.org/commons/beanutils/api/index.html</a>.
More information about using indexed and mapped properties in Struts can
be found in the FAQ describing <a href="../faqs/indexedprops.html">
<em>Indexed Properties, Mapped Properties,
and Indexed Tags</em>
</a>.
</p>
</div>
<h2 id="form_input">3.3.2 Input Field Types Supported</h2>
<div class="indent">
<p>
Struts defines HTML tags for all of the following types of input fields,
with hyperlinks to the corresponding reference information.
</p>
<ul>
<li>
<a href="struts-html.html#checkbox">checkboxes</a>
</li>
<li>
<a href="struts-html.html#hidden">hidden</a> fields
</li>
<li>
<a href="struts-html.html#password">password</a> input fields
</li>
<li>
<a href="struts-html.html#radio">radio</a> buttons
</li>
<li>
<a href="struts-html.html#reset">reset</a> buttons
</li>
<li>
<a href="struts-html.html#select">select</a> lists with embedded
option or options items
</li>
<li>
<a href="struts-html.html#option">option</a>
</li>
<li>
<a href="struts-html.html#options">options</a>
</li>
<li>
<a href="struts-html.html#submit">submit</a> buttons
</li>
<li>
<a href="struts-html.html#text">text</a> input fields
</li>
<li>
<a href="struts-html.html#textarea">textareas</a>
</li>
</ul>
<p>
In every
case, a field tag must be nested within a <code>form</code> tag, so that
the field knows what bean to use for initializing displayed values.
</p>
</div>
<h2 id="presentation_tags">3.3.3 Other Useful Presentation Tags</h2>
<div class="indent">
<p>
There are several tags useful for creating presentations, consult the
documentation on each specific tag library, along with the Tag Developers
Guides, for more information:
</p>
<ul>
<li>
[logic] <a href="struts-logic.html#iterate">iterate</a> repeats its
tag body once for each element of a specified collection (which can be an
Enumeration, a Hashtable, a Vector, or an array of objects).
</li>
<li>
[logic] <a href="struts-logic.html#present">present</a> depending on
which attribute is specified, this tag checks the current request, and
evaluates the nested body content of this tag only if the specified value
is present.
Only one of the attributes may be used in one occurrence of this tag,
unless you use the property attribute, in which case the name attribute
is also required.
The attributes include cookie, header, name, parameter, property, role,
scope, and user.
</li>
<li>
[logic] <a href="struts-logic.html#notPresent">notPresent</a> the
companion tag to present, notPresent provides the same functionality when
the specified attribute is not present.
</li>
<li>
[html] <a href="struts-html.html#link">link</a> generates a HTML
<a> element as an anchor definition or a hyperlink to the specified
URL, and automatically applies URL encoding to maintain session state in
the absence of cookie support.
</li>
<li>
[html] <a href="struts-html.html#img">img</a> generates a HTML
<img> element with the ability to dynamically modify the URLs
specified by the "src" and "lowsrc" attributes in the same manner that
<html:link> can.
</li>
<li>
[bean] <a href="struts-bean.html#parameter">parameter</a>
retrieves the value of the specified request parameter, and defines the
result as a page scope attribute of type String or String[].
</li>
</ul>
</div>
<h2 id="form_validation">3.3.4 Automatic Form Validation</h2>
<div class="indent">
<p>
In addition to the form and bean interactions described above, Struts
offers an additional facility to validate the input fields it has received.
To utilize this feature, override the following method in your ActionForm
class:
</p>
<pre>
<code>
validate(ActionMapping mapping,
HttpServletRequest request);
</code>
</pre>
<p>
The <code>validate</code> method is called by the controller servlet after
the bean properties have been populated, but before the corresponding
action class's <code>execute</code> method is invoked.
The <code>validate</code> method has the following options:
</p>
<ul>
<li>
Perform the appropriate validations and find no problems -- Return
either <code>null</code> or a zero-length ActionErrors instance,
and the controller servlet will proceed to call the
<code>perform</code> method of the appropriate <code>Action</code> class.
</li>
<li>
Perform the appropriate validations and find problems -- Return an
ActionErrors instance containing <code>ActionError</code>'s, which
are classes that contain the error message keys (into the
application's <code>MessageResources</code> bundle) that should be
displayed.
The controller servlet will store this array as a request attribute
suitable for use by the <code><html:errors></code> tag, and
will forward control back to the input form (identified by the
<code>input</code> property for this <code>ActionMapping</code>).
</li>
</ul>
<p>
As mentioned earlier, this feature is entirely optional.
The default implementation of the <code>validate</code> method returns
<code>null</code>, and the controller servlet will assume that any
required validation is done by the action class.
</p>
<p>
One common approach is to perform simple, prima facia validations using
the ActionForm <code>validate</code> method, and then handle the
"business logic" validation from the Action.
</p>
<p>
The Struts Validator, covered in the next section, may be used to easily
validate ActionForms.
</p>
</div>
<h2 id="validator">3.3.5 The Struts Validator</h2>
<div class="indent">
<p>
Configuring the Validator to perform form validation is easy.
</p>
<ol>
<li>
The ActionForm bean must extend ValidatorForm
</li>
<li>
The form's JSP must include the
<a href="struts-html.html"><html:javascript></a> tag for client
side validation.
</li>
<li>
You must define the validation rules in an xml file like this:
<pre>
<code>
<form-validation>
<formset>
<form name="logonForm">
<field property="username" depends="required">
<msg name="required" key="error.username"/>
</field>
</form>
</formset>
</form-validation>
</code>
</pre>
The msg element points to the message resource key to use when generating the error message.
</li>
<li>Lastly, you must enable the ValidatorPlugin in the struts-config.xml file like this:
<pre>
<code>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>
</code>
</pre>
</li>
</ol>
<p>
<strong>Note:</strong> If your required form property is one of the Java object representations of
primitive types (ie. java.lang.Integer), you must set the ActionServlet's convertNull init.
parameter to true. Failing to do this will result in the required validation not being performed
on that field because it will default to 0.
</p>
<p>
For more about the Struts Validator, see the
<a href="./dev_validator.html">Developers Guide</a>.
</p>
</div>
<h2 id="other_presentations">3.4 Other Presentation Techniques</h2>
<div class="indent">
<p>
Although the look and feel of your application can be completely
constructed based on the standard capabilities of JSP and the Struts
custom tag library, you should consider employing other techniques that
will improve component reuse, reduce maintenance efforts, and/or reduce
errors.
Several options are discussed in the following sections.
</p>
</div>
<h2 id="custom_tags">3.4.1 Application-Specific Custom Tags</h2>
<div class="indent">
<p>
Beyond using the custom tags provided by the Struts library, it is easy
to create tags that are specific to the application you are building, to
assist in creating the user interface. The MailReader example application included with
Struts illustrates this principle by creating the following tags unique to
the implementation of this application:
</p>
<ul>
<li>
<strong>checkLogon</strong> - Checks for the existence of a particular session
object, and forwards control to the logon page if it is missing. This is
used to catch cases where a user has bookmarked a page in the middle of
your application and tries to bypass logging on, or if the user's session
has been timed out. (Note that there are better ways to authenticate users; the
checkLogon tag is simply meant to demonstrate writing your own custom tags.) </li>
<li>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -