Multiple Struts Configuration Files Tutorial
Join the DZone community and get the full member experience.
Join For FreeYour struts-config.xml file is large and it is hard to maintain am I right? Not anymore you can easily break it into multiple xml files like this.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>StrutsExample2</display-name> <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-config2.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
By having multiple struts configuration files it will be easy to maintain and debug. Here we work with a single module, so you can split the configuration file according to your convenience. During the startup the ActionServlet will read all the configuration files and create a database of configuration objects, which it will later refer while processing the request.
Let's see how it works. The struts-config.xml files contains the following piece of code.
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action path="/sample1" type="com.vaannila.Sample1Action"> <forward name="success" path="/sample1.jsp" /> </action> </action-mappings> </struts-config>
The struts-config2.xml file contains the following code.
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action path="/sample2" type="com.vaannila.Sample2Action"> <forward name="success" path="/sample2.jsp" /> </action> </action-mappings> </struts-config>
In the sample action classes we simply return "success". So according to the configuration when the request URI is sample1 the user will be forwarded to sample1.jsp and when the request URI is sample2 the user will be forwarded to the sample2.jsp page.
Now let's try with the request URI sample1. In the index.jsp page we forward the request to the URI sample1.
<jsp:forward page="sample1.do"/>
When you execute the application the sample1.jsp is displayed to the user.
In the same way you can try with the request URI sample2.
If you are working with multiple modules in your project, then you can have one configuration file for each modules. Let's say the project has two modules admin and reports. You access the admin screens using the URI admin/admin-module1 and reports using the URI report/report-1. Here the admin and report are two different modules. The following code shows how to create a seperate configuration file for each module.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>StrutsExample2</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config/admin</param-name> <param-value>/WEB-INF/struts-config-admin.xml</param-value> </init-param> <init-param> <param-name>config/report</param-name> <param-value>/WEB-INF/struts-config-report.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Here the param-name should be config/moduleName and the param-value should be the corresponding configuration file. If you have more than one file for that particular module you can add them seperated by commas.
The struts configuration for admin module is done in the struts-config-admin.xml file.
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action path="/admin1" type="com.vaannila.admin.AdminAction"> <forward name="success" path="/admin.jsp"/> </action> </action-mappings> </struts-config>
In the AdminAction class we simply forward "success". The important thing to note is that in the configuration file the value of the path attribute is /admin1 and to invoke this we need to specify admin/admin1.do as the URI in the jsp page because it is there inside the admin module.
In the jsp page.
<jsp:forward page="admin/admin1.do"/>
On executing the example the user will be forwarded to the admin.jsp page which should be placed inside the admin directory.
The directory structure of the example look like this.
You can download the source code of this example by clicking on the Download link below.
Source: Download
War: Download
Opinions expressed by DZone contributors are their own.
Comments