Configure web.xml for Struts | specify struts-config.xml in Deployment Descriptor

One of benefits using the Struts framework is that application’s configuration can be specified in an external centralised configuration file (struts-config.xml) instead of being hard coded into the application. This simplifies development because changes can be made to the application without having to recompile any code. Struts configure files (struts-config.xml) are specified in the web.xml as shown below
<web-app>
<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>
......
</web-app>


The Struts configuration file is specified by declaring config parameter is a servlet initialization parameter . The config parameter must be set to your configuration file with the relative path of Web application , which will be the protected /WEB-INF/ directory so that it can be accessed only from server-side applications.
Struts supports the use of multiple configuration files. Configuration settings can be broken down into 2 or more files, which allows teams of developers to work on an application in parallel without having the configuration file be a point of contention. Multiple configuration files can be specified in web.xml as shown below seperated by comma . If there is any overlap among the files’ settings, Struts will use the last settings specified. For example, if struts-config.xml specifies a setting and then struts-config2.xml specifies the same setting with a different value, the setting in struts-config2.xml will override the one in struts-config.xml, provided that file struts-config2.xml is loaded after file struts-config.xml.
The following configuration shows the how to configure multiple configuration files.
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml, /WEB-INF/struts-config2.xml, /WEB-INF/struts-config3.xml,
</param-value>
</init-param>

Struts version 1.1 added support for application modules. The module feature allows applications to be broken as mini application inside a large application. For this we have to create a separate configuration file for each module, as shown below
<servlet>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>

<init-param>
<param-name>config/ModuleA</param-name>
<param-value>/WEB-INF/struts-config-moduleB.xml</param-value>
</init-param>

<init-param>
<param-name>config/ModuleB</param-name>
<param-value>/WEB-INF/struts-config-moduleA.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>


Note : For module configuration files, parameter names must begin with config/ in order for Struts to recognize them. eg. config/moduleA , config/moduleB . Following / is the logic name for the modules.



0 comments:

Post a Comment

 
Copyright 2011 Java Online