"An author doesn't necessarily understand the meaning of his own story better than anyone else."
Before you can build an application, you need to lay a solid foundation. There are several setup tasks you need to perform before deploying your application. These include components in the configuration file and in the Web Application Deployment Descriptor.
section covered writing the
portions of the configuration file.
These elements usually play an important role in the
development of an
The other elements in a configuration file tend to be
you set them once and leave them alone.
These "static" configuration elements are:
element allows you to configure
Many of the controller parameters were previously defined
initialization parameters in your
file but have been
moved to this section of
in order to allow
different modules in the same web application to be
For full details on available parameters see the
or the list below.
<forward>element is mapped to a context-relative URL when it starts with a slash. This value may consist of any combination of the following:
trueif you want the
<action>elements to be the name of a local or global
ActionForward, which will then be used to calculate the ultimate URL. Set to
falseto treat the
<action>elements as a module-relative path to the resource to be used as the input form. [false] (optional)
trueif you want a
Localeobject stored in the user's session if not already present. [true] (optional)
trueif you want the controller to add HTTP headers for defeating caching to every response from this module. [false] (optional)
pageattribute of custom tags using it is mapped to a context-relative URL of the corresponding resource. This value may consist of any combination of the following:
RequestProcessorsubclass to be used with this module. [org.apache.struts.chain.ComposableRequestProcessor] (optional)
This example uses the default values for several controller parameters. If you only want default behavior you can omit the controller section altogether.
<controller processorClass="org.apache.struts.action.RequestProcessor" contentType="text/html"/>;
The framework has built in support for
You can define one or more
for your webapp; modules can define their own resource
Different bundles can be used simultaneously in your
application, the 'key'
attribute is used to specify the desired bundle.
falseto display missing resource keys in your application like ' ???keyname??? ' instead of
null. [true] (optional)
trueif escape processing should be performed on the error message string. [true] (optional)
<message-resources parameter="MyWebAppResources" null="false" />
This would set up a message resource bundle provided in
under the default key.
Missing resource keys would be displayed as '
implementation can operate in one of three modes:
The mode can be configured in the struts-config.xml
(for more details see
<message-resources parameter="MyWebAppResources"> <set-property key="mode" value="JSTL"/> </message-resources>
Struts PlugIns are configured using the
element within the configuration file.
This element has only one valid attribute, 'className',
which is the fully
qualified name of the Java class which implements the
For PlugIns that require configuration themselves, the
element is available.
This is an example using the Tiles plugin:
<plug-in className="org.apache.struts.tiles.TilesPlugin"> <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/> </plug-in>
Very little is required in order to start taking advantage of the module feature. Just go through the following steps:
Back in version 1.0, a few "boot-strap" options were placed
file, and the bulk of the configuration was done in a
Obviously, this wasn't ideal for a team environment, since
had to share the same configuration file.
Since version 1.1, you have two options: you can list multiple struts-config files as a comma-delimited list, or you can subdivide a larger application into modules.
With the advent of modules, a given module has its own configuration file. This means each team (each module would presumably be developed by a single team) has their own configuration file, and there should be a lot less contention when trying to modify it.
Since version 1.0, you listed your configuration file as an
parameter to the action servlet in
This is still done since version 1.1, but the parameter can
In order to tell the framework machinery about your
different modules, you
specify multiple 'config' initialization parameters, with
a slight twist.
You'll still use 'config' to tell the ActionServlet about
module, however, for each additional module, you will
list an initialization parameter named "config
the prefix for your module (this gets used when
determining which URIs fall
under a given module, so choose something meaningful!).
... <init-param> <param-name>config</param-name> <param-value>/WEB-INF/conf/struts-default.xml</param-value> </init-param> <init-param> <param-name>config/module1</param-name> <param-value>/WEB-INF/conf/struts-module1.xml</param-value> </init-param> ...
Here we have two modules.
One happens to be the "default" module, identified by the
"config", and the other will be using the module prefix
on the param-name it was given ("config/module1").
The controller is configured to find the respective
(which is the recommended place to put all configuration
would be equivalent to what most folks call
I just like the symmetry of having all my module
configuration files being
If you'd like to vary where the pages for each module are stored, see the forwardPattern setting for the Controller.
There are three approaches for switching from one module to another.
org.apache.struts.actions.SwitchActionfrom the Extras JAR,
<forward>(global or local) and specify the contextRelative attribute with a value of true,
You can use
... <action-mappings> <action path="/toModule" type="org.apache.struts.actions.SwitchAction"/> ... </action-mappings> ...
Now, to change to ModuleB, we would use a URI like this:
If you are using the "default" module as well as "named" modules (like "/moduleB"), you can switch back to the "default" module with a URI like this:
Here's an example of a global forward:
<global-forwards> <forward name="toModuleB" contextRelative="true" path="/moduleB/index.do" redirect="true"/> ... </global-forwards>
You could do the same thing with a local forward declared in an ActionMapping:
<action-mappings> <action ... > <forward name="success" contextRelative="true" path="/moduleB/index.do" redirect="true"/> </action> ... </action-mappings>
Using the module parameter with a hyperlink tag is even simpler:
<html:link module="/moduleB" path="/index.do"/>
That's all there is to it! Happy module-switching!
The final step in setting up the application is to
application deployment descriptor (stored in file
) to include all the framework or Taglib components that
Using the deployment descriptor for the example
application as a guide,
we see that the following entries need to be created or
Add an entry defining the action servlet itself, along with the appropriate initialization parameters. Such an entry might look like this:
<servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
The initialization parameters supported by the action servlet are described below. (You can also find these details in the Javadocs for the ActionServlet class.) Square brackets describe the default values that are assumed if you do not provide a value for that initialization parameter.
java.lang.Integer) will default to null (rather than 0). (Since version 1.1) [false]
org.apache.commons.digester.RuleSetinstances that should be added to the
Digesterthat will be processing
struts-config.xmlfiles. By default, only the
RuleSetfor the standard configuration elements is loaded. (Since version 1.1)
- The framework will not
operate correctly if you define more than one
element for a controller
servlet, or a subclass of the standard controller servlet
The controller servlet
be a web application
Note: The material in this section is not specific to Struts. The configuration of servlet mappings is defined in the Java Servlet Specification. This section describes the most common means of configuring a application.
There are two common approaches to defining the URLs that will be processed by the controller servlet -- prefix matching and extension matching. An appropriate mapping entry for each approach will be described below.
Prefix matching means that you want all URLs that start (after the context path part) with a particular value to be passed to this servlet. Such an entry might look like this:
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>/do/*</url-pattern> </servlet-mapping>
which means that a request URI to match the
described earlier might look like this:
is the context path under which your
application is deployed.
Extension mapping, on the other hand, matches request URIs
action servlet based on the fact that the URI ends with a
followed by a defined set of characters.
For example, the JSP processing servlet is mapped
pattern so that it is called to process
every JSP page that is requested.
To use the
extension (which implies "do something"),
the mapping entry would look like this:
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
and a request URI to match the
earlier might look like this:
- The framework will not
operate correctly if you define more than one
element for the controller
WARNING - If you are using the new module support since version 1.1, you should be aware that only extension mapping is supported.
The Servlet 2.3 and 2.4 specifications simplify the
deployment and configuration of tag libraries.
All that's now required to install the Struts tag
libraries is to copy
directory and reference the tags in
your code like this:
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
Note that you
must use the full uri
defined in the various
tlds (see the
so that the container knows where to find the tag's class
You don't have to alter your
file or copy tlds into any
Of course, the configuration techniques use for older containers do still work.
To use the framework, you must copy
(and all of the
files) into your
directory. To use Struts Taglib,
you must also copy the
Likewise, to use other optional components, like
Extras, Tiles, or the Validator, copy the corresponding
Many servlet containers and application servers
for sharing JAR files across multiple web applications
on them. For example, Tomcat 4.1 allows you to put JAR
directories, and the classes
in those JAR files will be available in all
the need to place them in every web application's
directory. Usually, the sharing is
accomplished by creating a separate class loader that
is the parent
of the class loader (created by your container) for
If you have multiple web applications based on the framework,
it is tempting
to consider taking advantage of this container
feature, and placing
and the various
files in the shared directory, rather than in each web
However, there are several potential, and actual,
problems with this approach:
In spite of these difficulties, it is possible that sharing the Struts and Commons JAR files might appear to work for you. However, this is NOT a supported configuration.
If you file a bug report for
exceptions, or similar situations
where it appears that the wrong version of a class is
the bug report will
be processed unless the
problem exists with the JAR files in their recommended
subdirectory of your webapp.
Since version 1.0, the logging functionality was fairly
limited. You could
set a debugging detail level with a servlet initialization
all log messages were written to wherever
output is sent by your servlet container. With Struts 1.1,
logging messages written by the framework, as well as the
that it utilizes, flow through an abstract wrapper called
which can be used as a wrapper around any logging
implementation. The most
common implementations used are simple logging to
or the built-in logging capabilities of JDK 1.4 or later
This section does not attempt to fully explain how Commons Logging is configured and used. Instead, it focuses on pertinent details of using Commons Logging in connection with the Struts Framework. For complete documentation on using Commons Logging, consult the documentation for the logging system you are using, plus the Commons Logging Javadocs.
Commons Logging provides fine-grained control over the
created by a
instance. By convention, the
instances for the framework (and the Commons packages in
general) are named the
fully qualified class name of the class whose messages are
Therefore, log messages created by the
class are, naturally
enough, directed to a logger named
The advantage of this approach is that you can configure
the level of detail
in the output you want from each class, individually.
However, it would be
a burden to be required to maintain such settings for
every possible class,
so the logging environment supports the notion of logging
as well. If a detail level configuration for a
particular class has not been set, the logging system
looks up the hierarchy
until it finds a configuration setting to use, or else
uses the default
detail level if no configuration for any level of the
hierarchy has been
explicitly set. In the case of our messages from
the logging system will look for explicit settings of the
in this order, until it finds one:
In a similar manner, the detail level for messages from
(from the Commons BeanUtils library) is set by
a search for configuration settings for:
You can seamlessly integrate logging from your own components into the same logging implementation that the framework and the Commons libraries use, by following the instructions in Section 4.11 . If you do this, you are strongly encouraged to follow the same naming convention for loggers (based on the class name of the messages being logged) for maximum configuration flexibility.