Multiple configuration file in Struts 2

We can break struts.xml file into multiple small files. At runtime there can be only one struts.xml for an application so other files have to be included in the default struts.xml file.

Syntax:

<struts>
       //Other attributes
      <include file="strutsfile1.xml"/>
      <include file="strutsfile2.xml"/>
      //Other attributes
</struts>

Example:

login.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
 
<html>
 <head>
   <title>Struts 2 Multiple Struts configuration files example</title>
 </head>
 <body>
	<h3>This is a Multiple Struts configuration files example.</h3>
 
	<h4>For Admin page <a href="adminUser" >click here</a>.</h4>
	<h4>For User page <a href="normalUser" >click here</a>.</h4>
 
 </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
 	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
        	org.apache.struts2.dispatcher.ng.
        	filter.StrutsPrepareAndExecuteFilter
        </filter-class>
	</filter>
 
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 
	<welcome-file-list>
	  <welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
 
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="user" extends="struts-default">		
	</package>
 
	<include file="struts-admin.xml"></include>
	<include file="struts-user.xml"></include>
 
</struts>

struts-admin.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="adminUser" extends="struts-default">
		<action name="adminUser" 
		   class="com.w3schools.action.AdminUser">
			<result name="success">/admin.jsp</result>
		</action>
	</package>
 
</struts>

struts-user.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="normalser" extends="struts-default">
		<action name="normalUser" 
		    class="com.w3schools.action.NormalUser">
			<result name="success">/user.jsp</result>
		</action>
	</package>
 
</struts>

AdminUser.java

/**
 * This class is used as an action class for Admin User.
 * @author w3schools
 */
public class AdminUser{
	//data member
	private String message;
 
	//business logic
	public String execute(){
		setMessage("Hello Admin.");
		return "success";	
	}	
 
	//getter setters
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
}

NormalUser.java

/**
 * This class is used as an action class for normal User.
 * @author w3schools
 */
public class NormalUser{
	//data member
	private String message;
 
	//business logic
	public String execute(){
		setMessage("Hello user.");
		return "success";	
	}	
 
	//getter setters
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
}

admin.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
 
<html>
  <head>
    <title>Struts 2 Multiple Struts configuration files example</title>
  </head>
  <body>
     <h3>This is a Multiple Struts configuration files example.</h3>
     <s:property value="message" />	 
  </body>
</html>

user.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
 <head>
   <title>Struts 2 Multiple Struts configuration files example</title>
 </head>
 <body>
   <h3>This is a Multiple Struts configuration files example.</h3>
   <s:property value="message" />	 
 </body>
</html>

Output:

struts 5 first
 
If admin page link is clicked.
struts 5 admin
 
If user page link is clicked.
struts 5 user
 
Download this example.
 
Next Topic: OGNL in struts 2 with example.
Previous Topic: Struts 2 Hello World example.