📄 newbie.fml
字号:
errors. You code your validations in the
<code>validator-rules.xml</code>
file. A working knowledge of
<a href="http://etext.lib.virginia.edu/helpsheets/regex.html">
regular
expressions</a>
is necessary to use this feature effectively. For more
information, see
<a href="../userGuide/dev_validator.html">
User Guide</a>
</p>
</answer>
</faq>
<faq id="avoidValidate">
<question>How can I avoid validating a form before data is entered?"</question>
<answer>
<p>
The simplest way is to have two actions. The first one has
the job of setting
the form data, i.e. a blank registration screen. The
second action in our
writes the registration data to the database. The
framework
would take care of invoking the validation and returning
the user to the
correct screen if validation was not complete.
</p>
<p>
The EditRegistration action in the Struts MailReader
application illustrates this:
</p>
<source>
<action path="/editRegistration"
type="org.apache.struts.webapp.example.EditRegistrationAction"
attribute="registrationForm"
scope="request"
validate="false">
<forward name="success path="/registration.jsp"/>
</action>
</source>
<p>
When the /editRegistration action is invoked, a
registrationForm is created and added to the request,
but its validate method is not called. The default value
of the
<code>validate</code>
attribute is
<code>true</code>
, so if you do not want an action to trigger form
validation, you need to remember
to add this attribute and set it to
<code>false</code>
.
</p>
</answer>
</faq>
<faq id="wizard">
<question>How can I create a wizard workflow?</question>
<answer>
<p>
The basic idea is a series of actions with next, back,
cancel
and finish actions with a common bean. Using a
LookupDispatchAction is
reccomended as it fits the design pattern well and can be
internationalized
easily. Since the bean is shared, each choice made will
add data to the
wizards base of information. A sample of struts-config.xml
follows:
</p>
<source>
<form-beans>
<form-bean name="MyWizard"
type="forms.MyWizard" />
</form-beans>
<!-- the first screen of the wizard (next action only
available) -->
<!-- no validation, since the finish action is not
available -->
<actions>
<action path="/mywizard1"
type="actions.MyWizard"
name="MyWizard"
validate="false"
input="/WEB-INF/jsp/mywizard1.jsp">
<forward name="next"
path="/WEB-INF/jsp/mywizard2.jsp" />
<forward name="cancel"
path="/WEB-INF/jsp/mywizardcancel.jsp" />
</action>
<!-- the second screen of the wizard (back, next and
finish) -->
<!-- since finish action is available, bean should
validated, note
validation should not necessarily validate if back action
requested, you
might delay validation or do conditional validation -->
<action path="/mywizard2"
type="actions.MyWizard"
name="MyWizard"
validate="true"
input="/WEB-INF/jsp/mywizard2.jsp">
<forward name="back"
path="/WEB-INF/jsp/mywizard1.jsp" />
<forward name="next"
path="/WEB-INF/jsp/mywizard3.jsp" />
<forward name="finish"
path="/WEB-INF/jsp/mywizarddone.jsp" />
<forward name="cancel"
path="/WEB-INF/jsp/mywizardcancel.jsp" />
</action>
<!-- the last screen of the wizard (back, finish and
cancel only) -->
<action path="/mywizard3"
type="actions.MyWizard"
name="MyWizard"
validate="true"
input="/WEB-INF/jsp/mywizard3.jsp">
<forward name="back"
path="/WEB-INF/jsp/mywizard2.jsp" />
<forward name="finish"
path="/WEB-INF/jsp/mywizarddone.jsp" />
<forward name="cancel"
path="/WEB-INF/jsp/mywizardcancel.jsp" />
</action>
</source>
<p>
The pieces of the wizard are as follows:
</p>
<p>
<strong>forms.MyWizard.java</strong>
- the form bean holding the information required
</p>
<p>
<strong>actions.MyWizard.java</strong>
- the actions of the wizard, note the use of
LookupDispatchAction allows for one action class with
several methods. All the
real work will be done in the 'finish' method.
</p>
<p>
<strong>mywizard[x].jsp</strong>
- the data collection jsp's
</p>
<p>
<strong>mywizarddone.jsp</strong>
- the 'success' page
</p>
<p>
<strong>mywizardcancel.jsp</strong>
- the 'cancel' page
</p>
</answer>
</faq>
<faq id="chaining">
<question>How can I 'chain' Actions?</question>
<answer>
<p>
Chaining actions can be done by simply using the proper
mapping in your
forward entries in the struts-config.xml file. Assume you
had the following
two classes:
</p>
<source><![CDATA[
/* com/AAction.java */
...
public class AAction extends Action
{
public ActionForward
execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws
Exception
{
// Do something
return mapping.findForward("success");
}
}
]]></source>
<source><![CDATA[
/* com/BAction.java */
...
public class BAction extends Action
{
public ActionForward
execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws
Exception
{
// Do something else
return mapping.findForward("success");
}
}
]]></source>
<p>
Then you can chain together these two actions with the
Struts
configuration as shown in the following excerpt:
</p>
<source><![CDATA[
...
<action-mappings type="org.apache.struts.action.ActionMapping">
<action path="/A"
type="com.AAction"
validate="false">
<forward name="success" path="/B.do" />
</action>
<action path="/B"
type="com.BAction"
scope="session"
validate="false">
<forward name="success" path="/result.jsp" />
</action>
</action-mappings>
...
]]></source>
<p>
Here we are assuming you are using a suffix-based (
<code>.do</code>
) servlet
mapping, which is recommended since module support
requires it. When you
send your browser to the web application and name the
action
<code>A.do</code>
(i.e.
<code>http://localhost:8080/app/A.do</code>
) it will
execute
<code>AAction.execute()</code>
, which will then forward to the
"success" mapping.
</p>
<p>
This causes the execution of
<code>BAction.execute()</code>
since the
<code><forward></code>
entry for "success" in the configuration file
uses the
<code>.do</code>
suffix.
</p>
<p>
Of course it is also possible to chain actions
programmatically, but the
power and ease of being able to "reroute" your web
application's structure
using the XML configuration file is much easier to
maintain.
</p>
<p>
As a rule, chaining Actions is
<strong>not</strong>
recommended.
If your business classes are properly factored, you should
be able to call
whatever methods you need from any Action, without
splicing them together
into a cybernetic Rube Goldberg device.
</p>
<p>
If you must chain Actions, be aware of the following:
calling the second Action from the first Action has the
same effect as calling the second
Action from scratch.
If both of your Actions change the properties of a
formbean,
the changes made by the first Action will be lost because
the framework calls the reset() method on
the formbean when the second Action is called.
</p>
</answer>
</faq>
<faq id="undocumented">
<question>If you would like to contribute, here is a list of popular but undocumented questions</question>
<answer>
<ul>
<li>How can I capture binary or formatted values, like
dates or telephone numbers?</li>
<li>Can I create dynamic ActionForwards?</li>
<li>How can I use my own (ActionForm, ActionForward,
ActionMapping, ActionServlet) class?</li>
</ul>
</answer>
</faq>
</part>
</faqs>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -