Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction🤔
2.
Multiple Configuration🎯
2.1.
Example🧑‍🏫
3.
Frequently Asked Questions
3.1.
What is the Struts framework?
3.2.
What is the use of '<include .../>' element?
3.3.
What is 'package' in Struts XML?
3.4.
What is Web Framework?
3.5.
What is API?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Multi Configuration

Author Sanchit Kumar
0 upvote

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❓

Struts

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

URL👉 - http://localhost:8080/struts2multiConfig/ 

Response💫 - 

Understanding - Multiple Configuration Files in struts2


URL👉 - http://localhost:8080/struts2multiConfig/ruby/RubyConfig 
Response💫 - 

ruby configuration output


URL👉 - http://localhost:8080/struts2multiConfig/java/JavaConfig 

Response💫 - 

java configuration output

Frequently Asked Questions

What is the Struts framework?

Struts is a free, open-source framework for web applications written in Java. Apache Software Foundation maintains this framework.

What is the use of '<include .../>' element?

'<include .../>' element is used to define multiple configuration files in struts configuration file struts.xml.

What is 'package' in Struts XML?

Packages are a logical configuration unit that can be used to group actions, results, result types, interceptors, and interceptor stacks.

What is Web Framework?

It is a software framework, also known as a web application framework (WAF), which helps in the creation of web applications, including web APIs, web services and web resources.

What is API?

API (Application Programming Interface) is a means of communication between two or more computer programs. It is a kind of software interface that provides a service to other software programs.

Conclusion

In this article, we learnt multiple configuration files are used when we are dealing with large applications in the struts2 web application framework to manage things easily.

See the official website for documentation and other information about Struts, and to learn about various Java frameworks, click here.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning Ninja! 🥷

Thank You. Coding Ninjas.
Live masterclass