Struts 2 Multiple Configuration Files Example

 Download this Project

In this article, we will learn how to use multiple configuration files in struts2. We can use include tag to add multiple config files into struts config. Below is the syntax for the include tag.

<struts>
    ---
<include file="path_to_the_config_file2" ></include>
    ---
</struts>

1. Development Environment 

  1. Eclipse
  2. JDK 1.8
  3. Tomcat 8.5
  4. Maven

2. Create a Maven Web Project

Below is my dynamic web project struts2-multiple-configs structure in eclipse. In this example, we are using struts 2.1.8 version.

This project has the following actions,

  • hello-config1.action: This action will is configured in the config1.xml and it will show you a message.
  • hello-config2.action: This action will is configured in the config2.xml and it will show you a message.
  • hello-config3.action: This action will is configured in the config3.xml and it will show you a message.

Code:

package com.simba.demo;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Simba
 *
 */
public class MultiConfigFileAction extends ActionSupport {

  private String message;

  public MultiConfigFileAction() {
  }

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }
 
  public String messageFromConfig1() {
    setMessage("Hello World From Config 1");
    return SUCCESS;
  }
  public String messageFromConfig2() {
    setMessage("Hello World From Config 2");
    return SUCCESS;
  }
  public String messageFromConfig3() {
    setMessage("Hello World From Config 3");
    return SUCCESS;
  }
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Multiple Configuration Files Example</title>
</head>
<body>
  <h1>Multiple Configuration Files Example</h1>
  <h3><s:property value="message"/></h3>
</body>
</html>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"classpath://struts-2.0.dtd">

<struts>
  <package name="config1" namespace="/" extends="struts-default">
    <action name="hello-config1" class="com.simba.demo.MultiConfigFileAction" method="messageFromConfig1">
      <result>/WEB-INF/jsp/helloworld.jsp</result>
    </action>
  </package>
</struts>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"classpath://struts-2.0.dtd">

<struts>
  <package name="config2" namespace="/" extends="struts-default">
    <action name="hello-config2" class="com.simba.demo.MultiConfigFileAction" method="messageFromConfig2">
      <result>/WEB-INF/jsp/helloworld.jsp</result>
    </action>
  </package>
</struts>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"classpath://struts-2.0.dtd">

<struts>
  <package name="config3" namespace="/" extends="struts-default">
    <action name="hello-config3" class="com.simba.demo.MultiConfigFileAction" method="messageFromConfig3">
      <result>/WEB-INF/jsp/helloworld.jsp</result>
    </action>
  </package>
</struts>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"classpath://struts-2.0.dtd">

<struts>
  <include file="config1.xml" />
  <include file="config2.xml" />
  <include file="config3.xml" />
</struts>
<?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"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">
  <display-name>struts2-multiple-configs</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>struts2-multiple-configs</groupId>
  <artifactId>struts2-multiple-configs</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.1.8</version>
    </dependency>
  </dependencies>
</project>

3. Testing

In your eclipse configure tomcat and run your application by selecting Run on server.