Introduction🤔
While creating a large application using the struts web application framework, one might face difficulties in managing the application with a single configuration file. The solution to this problem is to make use of multiple configuration files to manage a large application.
So, let us learn how to create multiple configuration files.
Are you ready❓
Multiple Configuration🎯
So far, we know multiple configuration files are created to manage large applications. Let us see how it is done with an example.
Example🧑🏫
First of all, set up the basic struts web application and name it as struts2multiConfig.
Step1🧑💻 - Create two modules, 'ruby' and 'java', and create their respective configuration files.
- struts-ruby.xml – Put all ruby module settings here.
- struts-java.xml – Put all java module settings here.
- struts.xml -Put default settings and include struts-ruby.xml and struts-java.xml
Note - Add the following in ruby.xml, java.xml and struts.xml in the starting [before <struts>]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
struts-ruby.xml
<struts>
<constant name="struts.devMode" value="true" />
<package name="ruby" namespace="/ruby" extends="struts-default">
<action name="RubyConfig" class="com.multiconfig.MultipleConfigurationAction">
<result name="SUCCESS">/ruby.jsp</result>
</action>
</package>
</struts>
struts-java.xml
<struts>
<constant name="struts.devMode" value="true" />
<package name="java" namespace="/java" extends="struts-default">
<action name="JavaConfig" class="com.multiconfig.MultipleConfigurationAction">
<result name="SUCCESS">/java.jsp</result>
</action>
</package>
</struts>
⭐So far, we have created multiple configuration files. Now we need to define them in the struts configuration file struts.xml using the <include .../> element.
struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
</package>
<include file="ruby/struts-ruby.xml"></include>
<include file="java/struts-java.xml"></include>
</struts>
Step2✨ - Create an action class, override the execute() method, and return SUCCESS.
MultipleConfigurationAction.java
package com.multiconfig;
import com.opensymphony.xwork2.ActionSupport;
public class MultipleConfigurationAction extends ActionSupport {
private static final long serialVersionUID = -3695898710628233998L;
@Override
public String execute() throws Exception {
return "SUCCESS";
}
}
Step3🧑🎨 - Create the JSP pages[View Components] for each module.
index.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Understanding - Multiple Configuration Files in struts2</h1>
<p>Config - </p>
<ul>
<li><a href="ruby/RubyConfig">Ruby</a></li>
<li><a href="java/JavaConfig">Java</a></li>
</ul>
</body>
</html>
ruby.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Ruby Configuration</title>
</head>
<body>
<h2>Hi Ninja! This result is from the Ruby Configuration file.</h2>
</body>
</html>
java.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Java Configuration</title>
</head>
<body>
<h2>Hi Ninja! This result is from the Java Configuration file.</h2>
</body>
</html>
Step4🕸 - Make changes to the web.xml file.
web.xml
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>struts2multiConfig</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
Step5 - Execution🦾
Start the server and hit the following URLs one by one
- http://localhost:8080/struts2multiConfig/
- http://localhost:8080/struts2multiConfig/ruby/RubyConfig
-
http://localhost:8080/struts2multiConfig/java/JavaConfig
We will get the response from different configuration files😁.
URL👉 - http://localhost:8080/struts2multiConfig/
Response💫 -
URL👉 - http://localhost:8080/struts2multiConfig/ruby/RubyConfig
Response💫 -
URL👉 - http://localhost:8080/struts2multiConfig/java/JavaConfig
Response💫 -