Your 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.
01.<?xml version="1.0" encoding="UTF-8"?>
02.<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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"version="2.5">
03.<display-name>StrutsExample2</display-name>
04.
05.<servlet>
06.<servlet-name>action</servlet-name>
07.<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
08.<init-param>
09.<param-name>config</param-name>
10.<param-value>/WEB-INF/struts-config.xml,
/WEB-INF/struts-config2.xml</param-value>
11.</init-param>
12.<load-on-startup>2</load-on-startup>
13.</servlet>
14.<servlet-mapping>
15.<servlet-name>action</servlet-name>
16.<url-pattern>*.do</url-pattern>
17.</servlet-mapping>
18.
19.<welcome-file-list>
20.<welcome-file>index.jsp</welcome-file>
21.</welcome-file-list>
22.</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.
01.<?xml version="1.0" encoding="ISO-8859-1" ?>
02.
03.<!DOCTYPE
struts-config PUBLIC
04."-//Apache
Software Foundation//DTD Struts Configuration 1.3//EN"
06.
07.<struts-config>
08.<action-mappings>
09.<action path="/sample1" type="com.vaannila.Sample1Action">
10.<forward name="success" path="/sample1.jsp" />
11.</action>
12.</action-mappings>
13.</struts-config>
The struts-config2.xml file contains the following code.
01.<?xml version="1.0" encoding="ISO-8859-1" ?>
02.
03.<!DOCTYPE
struts-config PUBLIC
04."-//Apache
Software Foundation//DTD Struts Configuration 1.3//EN"
06.
07.<struts-config>
08.
09.<action-mappings>
10.<action path="/sample2" type="com.vaannila.Sample2Action">
11.<forward name="success" path="/sample2.jsp" />
12.</action>
13.</action-mappings>
14.
15.</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 URIsample1.
1.<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 theadmin and report are two different modules. The following code shows how to create a
seperate configuration file for each module.
01.<?xml version="1.0" encoding="UTF-8"?>
02.<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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"version="2.5">
03.<display-name>StrutsExample2</display-name>
04.
05.<servlet>
06.<servlet-name>action</servlet-name>
07.<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
08.<init-param>
09.<param-name>config/admin</param-name>
10.<param-value>/WEB-INF/struts-config-admin.xml</param-value>
11.</init-param>
12.<init-param>
13.<param-name>config/report</param-name>
14.<param-value>/WEB-INF/struts-config-report.xml</param-value>
15.</init-param>
16.<load-on-startup>2</load-on-startup>
17.</servlet>
18.
19.<servlet-mapping>
20.<servlet-name>action</servlet-name>
21.<url-pattern>*.do</url-pattern>
22.</servlet-mapping>
23.
24.<welcome-file-list>
25.<welcome-file>index.jsp</welcome-file>
26.</welcome-file-list>
27.</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.
01.<?xml version="1.0" encoding="ISO-8859-1" ?>
02.
03.<!DOCTYPE
struts-config PUBLIC
04."-//Apache
Software Foundation//DTD Struts Configuration 1.3//EN"
06.
07.<struts-config>
08.<action-mappings>
09.<action path="/admin1" type="com.vaannila.admin.AdminAction">
10.<forward name="success" path="/admin.jsp"/>
11.</action>
12.</action-mappings>
13.</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 specifyadmin/admin1.do as the URI in the jsp page because it is there inside the admin module.
In the jsp page.
1.<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.
No comments:
Post a Comment