Wednesday, October 21, 2009

Recipe 2.4. Using Multiple Struts Configuration Files










Recipe 2.4. Using Multiple Struts Configuration Files




Problem



You want to break
apart a large struts-config.xml file into
smaller files for improved organization and easier maintenance,
particularly in a team environment.





Solution



Split your monolithic struts-config.xml into
multiple configuration files. Each file must be well-formed and valid
according to the struts-config XML DTD.
Reference these files as the value for the config
initialization parameter of the ActionServlet in
the web.xml file as shown in Example 2-9.




Example 2-9. Multiple config files (single module)

<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,
/WEB-INF/struts-config-2.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>





When the ActionServlet is loaded, Struts will
merge the results of the specified configuration files into a single
in-memory configuration.





Discussion



For anything other than the most trivial applications, the
struts-config.xml file tends to get large and
unwieldy. Many applications may have action
elements numbering in the hundreds. Combine this with the use of a
locking source control system, and soon you will have developers in
constant contention for the same file.



Struts 1.1 introduced support for multiple configuration files. Each
configuration file must be a valid XML file and conform to the
struts-config XML
Document Type Definition (DTD). You declare the set of files as the
parameter value for the config
ActionServlet initialization parameter in your
web.xml file. You specify the files as a
comma-separated list of file paths. At runtime, the files are merged
in memory into a single configuration set. If duplicate elements
existfor example, a form bean declaration with the same value
for the name attributethe value read from
the last configuration file listed takes precedence.



Just because you are using multiple configuration files
doesn't necessarily mean that
you're using Struts modules. In fact, you can have
multiple configuration files for one individual module. In the
Solution above, the param-name element value of
config dictates to the
ActionServlet the path to the Struts configuration
files for the default module. Additional modules are indicated by
using a param-name value of
config/module-name.
Example 2-10 shows a Struts
ActionServlet declaration from a
web.xml file with a default module and two
additional modules. The default module uses two configuration files:
module1 uses only one configuration file, and
module2 uses three configuration files.




Example 2-10. Multiple config files (multiple modules)

<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-default-config.xml,
/WEB-INF/struts-default-config-2.xml
</param-value>
</init-param>
<init-param>
<param-name>config/module1</param-name>
<param-value>
/WEB-INF/struts-module1-config.xml
</param-value>
</init-param>
<init-param>
<param-name>config/module2</param-name>
<param-value>
/WEB-INF/struts-module2-config.xml,
/WEB-INF/struts-module2-config-2.xml,
/WEB-INF/struts-module2-config-3.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>





If you are doing team development, consider splitting your
configuration files based on functional area, use cases, or user
stories. Individual team members can focus on their areas of
responsibility without bumping into the rest of the team.





See Also



Recipe 2.5 details the nuances of using
Struts modules. Recipe 1.8 describes how to
generate the struts configuration files automatically.












    No comments:

    Post a Comment