📄 building_controller.html
字号:
Should the ActionServlet redirect to the resource instead of forward?
</li>
</ul>
<p>
<code>
<strong><action-mappings></strong>
</code>
<br />
This section contains your action definitions.
You use an <code><action></code> element for each of the mappings
you would like to define.
Most action elements will define at least the following attributes:
</p>
<ul>
<li>
<code>path</code>: The application context-relative path to the
action.
</li>
<li>
<code>type</code>: The fully qualified java classname of your
Action class.
</li>
<li>
<code>name</code>: The name of your
<code><form-bean></code> element to use with this action
</li>
</ul>
<p>Other often-used attributes include:</p>
<ul>
<li>
<code>parameter</code>: A general-purpose attribute often used by
"standard" Actions to pass a required property.
</li>
<li>
<code>roles</code>: A comma-delimited list of the user security roles
that can access this mapping.
</li>
</ul>
<p>
For a complete description of the elements that can be used with the
<code>action</code> element, see the
<a href="http://struts.apache.org/dtds/struts-config_1_2.dtd">
Struts Configuration DTD</a> and the
<a href="../api/org/apache/struts/action/ActionMapping.html">ActionMapping
documentation</a>.
</p>
</div>
<h2 id="action_mapping_example">4.8.1 ActionMapping Example</h2>
<div class="indent">
<p>
Here's a mapping entry based on the MailReader example
application. The MailReader application now uses DynaActionForms.
But in this example, we'll show a conventinal
ActionForm instead, to illustrate the usual workflow.
Note that the entries for all the other actions are left out:
</p>
<pre>
<code><struts-config>
<form-beans>
<form-bean
name="logonForm"
type="org.apache.struts.webapp.example.LogonForm" />
</form-beans>
<global-forwards
type="org.apache.struts.action.ActionForward">
<forward
name="logon"
path="/logon.jsp"
redirect="false" />
</global-forwards>
<action-mappings>
<action
path="/logon"
type="org.apache.struts.webapp.example.LogonAction"
name="logonForm"
scope="request"
input="/logon.jsp"
unknown="false"
validate="true" />
</action-mappings>
</struts-config>
</code>
</pre>
<p>
First the form bean is defined.
A basic bean of class "<code>org.apache.struts.webapp.example.LogonForm</code>"
is mapped to the logical name "<code>logonForm</code>".
This name is used as a request attribute name for the form
bean.
</p>
<p>
The "<code>global-forwards</code>" section is used to create logical name
mappings for commonly used presentation pages.
Each of these forwards is available through a call to your action mapping
instance, i.e. <code>mapping.findForward("logicalName")</code>.
</p>
<p>
As you can see, this mapping matches the path <code>/logon</code>
(actually, because the MailReader example application uses extension
mapping, the request URI you specify in a JSP page would end in
<code>/logon.do</code>).
When a request that matches this path is received, an instance of the
<em>LogonAction</em> class will be created (the first time only) and used.
The controller servlet will look for a bean in request scope under key
<code>logonForm</code>, creating and saving a bean of the specified class
if needed.
</p>
<p>
Optional but very useful are the local "<code>forward</code>" elements.
In the MailReader example application, many actions include a local
"success" and/or "failure" forward as part of an action mapping.
</p>
<pre>
<code><!-- Edit mail subscription -->
<action
path="/editSubscription"
type="org.apache.struts.webapp.example.EditSubscriptionAction"
name="subscriptionForm"
scope="request"
validate="false">
<forward
name="failure"
path="/mainMenu.jsp"/>
<forward
name="success"
path="/subscription.jsp"/>
</action>
</code>
</pre>
<p>
Using just these two extra properties, the Action classes are almost
totally independent of the actual names of the presentation pages.
The pages can be renamed (for example) during a redesign, with negligible
impact on the Action classes themselves.
If the names of the "next" pages were hard coded into the Action classes,
all of these classes would also need to be modified.
Of course, you can define whatever local <code>forward</code> properties
makes sense for your own application.
</p>
<p>
The Struts configuration file includes several other elements that you
can use to customize your application.
See "<a href="configuration.html">Configuring Applications</a>" for details.
</p>
</div>
<h2 id="module_config-use_actions">4.9 Using ActionMappings for Pages</h2>
<div class="indent">
<p>
Fronting your pages with ActionMappings is <em>essential</em> when using
modules, since doing so is the only way you involve the
controller in the request -- and you want to!
The controller puts the application configuration in the request,
which makes available all of your module-specific configuration data
(including which message resources you are using, request-processor,
datasources, and so forth).
</p>
<p>
The simplest way to do this is to use the <code>forward</code> property
of the ActionMapping:
</p>
<pre><action path="/view" forward="/view.jsp"/></pre>
</div>
<h2 id="action_mapping_wildcards">4.10 Using Wildcards in ActionMappings</h2>
<div class="indent">
<p>
[Since Struts 1.2.0] As a Struts application grows in size, so will the number of action
mappings. Wildcards can be used to combine similiar mappings into one
more generic mapping.
</p>
<p>
The best way to explain wildcards is to show an example and walk through
how it works. This example modifies the previous mapping in the <a href="#action_mapping_example">ActionMapping Example</a> section to use
wildcards to match all pages that start with <code>/edit</code>:
</p>
<pre>
<code><!-- Generic edit* mapping -->
<action
path="/edit*"
type="org.apache.struts.webapp.example.Edit{1}Action"
name="{1}Form"
scope="request"
validate="false">
<forward
name="failure"
path="/mainMenu.jsp"/>
<forward
name="success"
path="/{1}.jsp"/>
</action>
</code>
</pre>
<p>
The "<code>*</code>" in the path attribute allows the mapping to match the
request URIs <code>/editSubscription</code>, <code>editRegistration</code>,
or any other URI that starts with
<code>/edit</code>, however <code>/editSubscription/add</code> would not be
matched. The part of
the URI matched by the wildcard will then be substituted into various
attributes of the action mapping and its action forwards replacing
<code>{1}</code>.
For the rest of the request, Struts will see the action mapping and its
action forwards containing the new values.
</p>
<p>
Wildcard patterns can contain one or more of the following special tokens:
</p>
<table>
<tr>
<td>
<code>*</code>
</td>
<td>
Matches zero or more characters excluding the
slash ('/') character.
</td>
</tr> <tr>
<td>
<code>**</code>
</td>
<td>
Matches zero or more characters including the
slash ('/') character.
</td>
</tr> <tr>
<td>
<code>\character</code>
</td>
<td>
The backslash character is used as an escape
sequence. Thus <code>\*</code> matches the character asterisk
('*'), and <code>\\</code>
matches the character backslash ('\').
</td>
</tr>
</table>
<p>
In the action mapping and action forwards, the wildcard-matched values can
be accessed with the token <code>{N}</code> where <code>N</code>
is a number from 1 to 9 indicating
which wildcard-matched value to substitute. The whole request URI can be
accessed with the <code>{0}</code> token.
</p>
<p>
The action mapping attributes that will accept wildcard-matched strings
are:
</p>
<ul>
<li>
<code>type</code>
</li>
<li>
<code>name</code>
</li>
<li>
<code>roles</code>
</li>
<li>
<code>parameter</code>
</li>
<li>
<code>attribute</code>
</li>
<li>
<code>forward</code>
</li>
<li>
<code>include</code>
</li>
<li>
<code>input</code>
</li>
</ul>
<p>
The action forward attributes that will accept wildcard-matched strings
are:
</p>
<ul>
<li>
<code>path</code>
</li>
</ul>
</div>
<h2 id="logging">4.11 Commons Logging Interface</h2>
<div class="indent">
<p>
Struts doesn't configure logging itself -- it's all done by
<a href="http://jakarta.apache.org/commons/">commons-logging</a>
under the covers.
The default algorithm is a search:
</p>
<ul>
<li>
If Log4J is there, use it.
</li>
<li>
If JDK 1.4 is there, use it.
</li>
<li>
Otherwise, use SimpleLog.
</li>
</ul>
<p>
The commons-logging interface is an <em>ultra-thin</em> bridge to many
different logging implementations.
The intent is to remove compile- and run-time dependencies on any
single logging implementation.
For more information about the currently-supported implementations,
please refer to the
<a href="http://jakarta.apache.org/commons/logging/api/index.html">
the description for the <code>org.apache.commons.logging</code>
package</a>.
</p>
<p>
Because Struts uses commons-logging and, therefore, includes the necessary
JAR files for <strong>you</strong> to use commons-logging, you've probably had the
occasional fleeting thought, <em>"Should I use
commons-logging?"</em>
The answer (surprise!) depends on the requirements for your particular
project.
If one of your requirements is the ability to easily change logging
implementations with zero impact on your application, then commons-logging
is a very good option.
</p>
<p>
<em>"Great! What do I do to get started using commons-logging in my own
code?"</em>
</p>
<p>
Using commons-logging in your own code is very simple - all you need are
two imports and a declaration for a logger.
Let's take a look:
</p>
<pre>
<code>package com.foo;
// ...
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
...
public class Foo {
// ...
private static Log log = LogFactory.getLog(Foo.class);
// ...
public void setBar(Bar bar) {
if (log.isTraceEnabled()) {
log.trace("Setting bar to " + bar);
}
this.bar = bar;
}
// ...
}
</code>
</pre>
<p>
The general idea is to instantiate a single logger per class and to
use a name for the logger which reflects where it's being used. The
example is constructed with the class itself. This gives the
logger the name of com.foo.Foo. Doing things this way lets you
easily see where the output is coming from, so you can quickly
pin-point problem areas. In addition, you are able to enable/disable
logging in a very fine-grained way.
</p>
<p>
For examples of using logging in Struts classes, see the
Action classes in the Struts MailReader example application.
</p>
</div>
<hr class="section" />
<div class="indent">
<p class="right">
Next: <a href="./configuration.html">Configuring Applications</a>
</p>
</div>
</div>
<!--end main-->
</div>
<!--end content-->
<div id="footer">
<img id="powered-logo" alt="Powered by Struts" src="../images/struts-power.gif" />
Copyright (c) 2000-2005, The Apache Software Foundation <span class="noprint">-
<a href="http://wiki.apache.org/struts/StrutsDocComments">Comments?</a>
</span>
</div>
<!--end footer-->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -