📄 newbie.fml
字号:
by declaring an instance of the same form bean name.
</p>
<p>The
<em>MailReader</em>
application, part of the Struts Applications
subrpoject, illustrates this design pattern nicely. Note
the following
definitions from the
<code>struts-config.xml</code>
file:
</p>
<pre>
...
<form-beans>
...
<-- Registration form bean -->
<form-bean name="registrationForm"
type="org.apache.struts.webapp.example.RegistrationForm"/>
...
</form-beans>
...
<action-mappings>
...
<-- Edit user registration -->
<action path="/editRegistration"
type="org.apache.struts.webapp.example.EditRegistrationAction"
name="registrationForm"
scope="request"
validate="false"/>
...
<-- Save user registration -->
<action path="/saveRegistration"
type="org.apache.struts.webapp.example.SaveRegistrationAction"
name="registrationForm"
input="registration"
scope="request"/>
...
</action-mappings>
</pre>
<p>Note the following features of this approach:</p>
<ul>
<li>Both the
<code>/editRegistration</code>
and
<code>/saveRegistration</code>
actions use the same form bean.
</li>
<li>When the
<code>/editRegistration</code>
action is entered, the
framework will have pre-created an empty form bean
instance, and passed it to
the
<code>execute()</code>
method. The setup action is free to
preconfigure the values that will be displayed when
the form is
rendered, simply by setting the corresponding form
bean properties.
</li>
<li>When the setup action completes configuring the
properties of the
form bean, it should return an
<code>ActionForm</code>
that points
at the page which will display this form. If you are
using the
Struts JSP tag library, the
<code>action</code>
attribute on your
<html:form> tag will be set to
<code>/saveRegistration</code>
in order for the form to be submitted to the
processing action.
</li>
<li>Note that the setup action (
<code>/editRegistration</code>
) turns off
validation on the form that is being set up. You will
normally want
to include this attribute in the configuration of your
setup actions,
because you are not planning to actually process the
results -- you
simply want to take advantage of the fact that the
framework will precreate
a form bean instance of the correct class for you.
</li>
<li>The processing action (
<code>/saveRegistration</code>
), on the other
hand, leaves out the
<code>validate</code>
attribute, which defaults
to
<code>true</code>
. This tells the framework to perform the validations
associated with this form bean before invoking the
processing action
at all. If any validation errors have occurred, the
framework will forward
back to your input page (technically, it forwards back
to an
<code>ActionForward</code>
named "registration" in this case, because
the example webapp uses the
<code>inputForward</code>
attribute in the
<code><controller></code>
element -- see the documentation
describing
<code>struts-config.xml</code>
for more information)
instead of calling your processing action.
</li>
</ul>
</answer>
</faq>
<faq id="noForm">
<question>Can I have an Action without a form?</question>
<answer>
<p>
Yes. If your
<code>Action</code>
does not need any data and it does not need to make any
data available to the view or controller component that it
forwards to, it doesn't need
a form. A good example of an
<code>Action</code>
with no
<code>ActionForm</code>
is the
<code>LogoffAction</code>
in the Struts MailReader application:
</p>
<pre>
<action path="/logoff"
type="org.apache.struts.webapp.example.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action>
</pre>
<p>
This action needs no data other than the user's session,
which it can get from the
<code>Request</code>
, and it doesn't need to prepare any view elements for
display,
so it does not need a form.
</p>
<p>
However, you cannot use the <html:form>
<strong>tag</strong>
without an ActionForm.
Even if you want to use the <html:form> tag with a
simple Action that does not require input,
the tag will expect you to use some type of ActionForm,
even if it is an empty subclass without any properties.
</p>
</answer>
</faq>
<faq id="requiredif">
<question>Can you give me a simple example of using the requiredif Validator rule?</question>
<answer>
<p>First off, there's an even newer Validator rule called
<code>validwhen</code>
,
which is almost certainly what you want to use, since it
is much easier and
more powerful. It will be available in the first release
after 1.1 ships.
The example shown below could be coded with validwhen as:
</p>
<pre>
<form name="medicalStatusForm">
<field
property="pregnancyTest" depends="validwhen">
<arg0 key="medicalStatusForm.pregnancyTest.label"/>
<var>
<var-name>test</var-name>
<var-value>((((sex == 'm') OR (sex == 'M')) AND
(*this* == null)) OR (*this* != null))</test>
</var>
</field>
</pre>
<p>Let's assume you have a medical information form with three
fields, sex, pregnancyTest, and testResult.
If sex is 'f' or 'F', pregnancyTest is required. If
pregnancyTest is not blank, testResult is required.
The entry in your validation.xml file would look like
this:
</p>
<pre>
<form name="medicalStatusForm">
<field
property="pregnancyTest" depends="requiredif">
<arg0 key="medicalStatusForm.pregnancyTest.label"/>
<var>
<var-name>field[0]</var-name>
<var-value>sex</var-value>
</var>
<var>
<var-name>fieldTest[0]</var-name>
<var-value>EQUAL</var-value>
</var>
<var>
<var-name>fieldValue[0]</var-name>
<var-value>F</var-value>
</var>
<var>
<var-name>field[1]</var-name>
<var-value>sex</var-value>
</var>
<var>
<var-name>fieldTest[1]</var-name>
<var-value>EQUAL</var-value>
</var>
<var>
<var-name>fieldValue[1]</var-name>
<var-value>f</var-value>
</var>
<var>
<var-name>fieldJoin</var-name>
<var-value>OR</var-value>
</var>
</field>
<field
property="testResult" depends="requiredif">
<arg0 key="medicalStatusForm.testResult.label"/>
<var>
<var-name>field[0]</var-name>
<var-value>pregnancyTest</var-value>
</var>
<var>
<var-name>fieldTest[0]</var-name>
<var-value>NOTNULL</var-value>
</var>
</field>
</form>
</pre>
</answer>
</faq>
<faq id="validate">
<question>When is the best time to validate input?</question>
<answer>
<p>
This is an excellent question. Let's step back a second
and think about a
typical mid to large size application. If we start from
the back end and work
toward the view we have:
</p>
<p>
1) Database: Most modern databases are going to validate
for required
fields, duplicate records, security constraints, etc.</p>
<p>
2) Business Logic: Here you are going to check for valid
data relationships
and things that make sense for the particular problem you
are triing to
solve.</p>
<p>
... This is where the framework comes into the picture, by
now the system should be
pretty well bulletproof. What we are going to do is make
validation friendlier
and informative. Rember it is OK to have duplicate
validations...</p>
<p>
3)
<code>ActionErrors validate(ActionMapping map,
HttpServletRequest req)</code>
is where you can do your validation and feed back to the
view,
information required to correct any errors.
<code>validate</code>
is run after
the form has been
<code>reset</code>
and after the
<code>ActionForm</code>
properties have been set from corresponding view based
input. Also remember you
can turn validation off with
<code>validate="false"</code>
in the
<code>action</code>
mapping in the
<code>struts-config.xml</code>
. This is done
by returning an
<code>ActionErrors</code>
collection with messages from your
<code>ApplicationResources.properties</code>
file.
</p>
<p>
Here you have access to the request so you can see what
kinds of action is
being requested to fine tune your validations. The <html:error>
tag
allows you to dump all errors on your page or a particular
error associated
with a particular property. The
<code>input</code>
attribute of the
<code>struts-config.xml</code>
<code>action</code>
allows you to send
validation errors to a particular jsp / html / tile page.
</p>
<p>
4) You can have the system perform low level validations
and client side
feedback using a
<code>ValidatorForm</code>
or its derivatives. This will
generate javascript and give instant feedback to the user
for simple data entry
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -