📄 newbie.fml
字号:
<p>
See also:
<a href="../userGuide/building_controller.html#map_action_form_classes">
Map-Backed ActionForms</a>
(since Struts 1.1)
</p>
</answer>
</faq>
<faq id="authenticate">
<question>How can I authenticate my users?</question>
<answer>
<p>
<a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24504.html">
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24504.html</a>
<br/>
<a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg22949.html">
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg22949.html</a>
</p>
</answer>
</faq>
<faq id="jsp">
<question>Do I have to use JSPs with my application?</question>
<answer>
<p>
The short answer to this question is: No, you are not
limited to
JavaServer Pages.
</p>
<p>
The longer answer is that you can use any type of
presentation technology
which can be returned by a web server or Java container.
The list includes but is not limited to:
</p>
<ul>
<li>
JavaServer Pages,
</li>
<li>
HTML pages,
</li>
<li>
WML files,
</li>
<li>
Java servlets,
</li>
<li>
Velocity templates, and
</li>
<li>
XML/XLST
</li>
</ul>
<p>
Some people even mix and match apparently unrelated
technologies,
like PHP, into the same web application.
</p>
</answer>
</faq>
<faq id="formbeans">
<question>Do ActionForms have to be true JavaBeans?</question>
<answer>
<p>ActionForms are added to a servlet scope (session or
request)
as beans. What this means is that, for certain
functionality to
be available, your ActionForms will have to follow a few
simple
rules.</p>
<p>First, your ActionForm bean must have a zero-arguments
constructor. This is required because the framework must
be able to
dynamically create new instances of your form bean class,
while
knowing only the class name. This is not an onerous
restriction,
however, because the framework will also populate your
form bean's
properties (from the request parameters) for you.</p>
<p>Second, the fields of your form bean are made available to
the
framework by supplying public getter and setter methods
that
follow the naming design patterns described in the
JavaBeans
Specification. For most users, that means using the
following
idiom for each of your form bean's properties:</p>
<pre>
private {type} fieldName;
public {type} getFieldName() {
return (this.fieldName);
}
public void setFieldName({type} fieldName) {
this.fieldName = fieldName;
}
</pre>
<p>
<strong>NOTE</strong>
- you
<em>MUST</em>
obey the capitalization
conventions shown above for your ActionForm properties to
be recognized.
The property name in this example is "fieldName", and that
must also be
the name of the input field that corresponds to this
property. A bean
property may have a "getter" method and a "setter" method
(in a form bean,
it is typical to have both) whose name starts with "get"
or "set",
followed by the property name with the first character
capitalized.
(For boolean properties, it is also legal to use "is"
instead of "get"
as the prefix for the getter method.)
</p>
<p>Advanced JavaBeans users will know that you can tell the
system
you want to use different names for the getter and setter
methods, by
using a
<code>java.beans.BeanInfo</code>
class associated with your form
bean. Normally, however, it is much more convenient to
follow the
standard conventions.
</p>
<p>
<strong>WARNING</strong>
- developers might be tempted to use one of
the following techniques, but any of them will cause your
property not
to be recognized by the JavaBeans introspection
facilities, and therefore
cause your applications to misbehave:
</p>
<ul>
<li>
<em>Using getter and setter method names that do not
match</em>
- if you have a
<code>getFoo()</code>
method for your
getter, but a
<code>setBar()</code>
method for your setter, Java
will not recognize these methods as referring to the
same property.
Instead, the language will think you have a read-only
property named
"foo" and a write-only property named "bar".
</li>
<li>
<em>Using more than one setter method with the same
name</em>
- The Java language lets you "overload" methods, as
long
as the argument types are different. For example, you
could have a
<code>setStartDate(java.util.Date date)</code>
method and a
<code>setStartDate(String date)</code>
method in the same class, and
the compiled code would know which method to call
based on the
parameter type being passed. However, doing this for
form bean
properties will prevent Java from recognizing that you
have a
"startDate" property at all.
</li>
</ul>
<p>There are other rules to follow if you want other features
of your form beans to be exposed,
especially in terms of indexed attributes and mapped attributes.
Specific rules are covered in detail in other areas of
the documentation, in particular,
<a href="indexedprops.html">
Indexed Properties, Mapped Properties, and Indexed Tags.
</a></p>
<p>For a complete explanation of what a JavaBean is, and
everything it can do,
see the
<a href="http://java.sun.com/products/javabeans/docs/beans.101.pdf">
JavaBeans Specification (version 1.01).</a></p>
</answer>
</faq>
<faq id="separate">
<question>Do I have to have a separate ActionForm bean for every HTML form?</question>
<answer>
<p>This is an interesting question. As a newbie, it is a good
practice to create a new
<code>ActionForm</code>
for each action
sequence. You can use
<code>DynaActionForm</code>
s to help reduce
the effort required, or use the code generation facilities
of your IDE.
</p>
<p>Some issues to keep in mind regarding reuse of form beans
are as follows:</p>
<ul>
<li>
<em>Validation</em>
- You might need to use different
validation rules depending upon the action that is
currently
being executed.
</li>
<li>
<em>Persistence</em>
- Be careful that a form populated in
one action is not
<strong>unexpectedly</strong>
reused in a
different action. Multiple
<code><form-bean></code>
entries in
<code>struts-config.xml</code>
for the same
<code>ActionForm</code>
subclass can help (especially if you
store your form beans in session scope).
Alternatively,
storing form beans in request scope can avoid
unexpected
interactions (as well as reduce the memory footprint
of your
application, because no server-side objects will need
to be
saved in between requests.
</li>
<li>
<em>Checkboxes</em>
- If you do as recommended and reset
your boolean properties (for fields presented as
checkboxes),
and the page you are currently displaying does not
have a
checkbox for every boolean property on the form bean,
the
undisplayed boolean properties will always appear to
have a
<code>false</code>
value.
</li>
<li>
<em>Workflow</em>
- The most common need for form bean
reuse is workflow. Out of the box, the framework has
limited support
for workflow, but a common pattern is to use a single
form bean
with all of the properties for all of the pages of a
workflow.
You will need a good understanding of the
environment (
<code>ActionForm</code>
s,
<code>Action</code>
s,
etc.) prior to being able to put together a smooth
workflow
environment using a single form bean.
</li>
</ul>
<p>As you get more comfortable, there are a few shortcuts you
can
take in order to reuse your
<code>ActionForm</code>
beans. Most of
these shortcuts depend on how you have chosen to implement
your
<code>Action</code>
/
<code>ActionForm</code>
combinations.
</p>
</answer>
</faq>
<faq id="prepopulate">
<question>How can I prepopulate a form?</question>
<answer>
<p>The simplest way to prepopulate a form is to have an
<code>Action</code>
whose sole purpose is to populate an
<code>ActionForm</code>
and forward
to the servlet or JSP to render that form back to the
client. A separate
<code>Action</code>
would then be use to process the submitted form fields,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -