DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Multiple Struts Configuration Files Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · Tutorial
Like (0)
Save
Tweet
Share
58.96K Views

Join the DZone community and get the full member experience.

Join For Free

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.

<?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.

Popular on DZone

  • 10 Easy Steps To Start Using Git and GitHub
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey
  • Introduction to Automation Testing Strategies for Microservices
  • How To Build an Effective CI/CD Pipeline

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: