Multiple namespaces example in Struts 2

Introduction

I will show you here how to create multiple namespaces in struts 2 web application. The benefit of namespace is the same file and action can be mapped to the multiple modules. The default namespace is “” – an empty string. A root namespace “/” is also supported.

The namespace attribute subdivides action configurations into logical modules, each with its own identifying prefix. Namespaces avoid conflicts between action names. Each namespace can have its own “menu” or “help” action, each with its own implementation. While the prefix appears in the browser URI, the tags are “namespace aware”, so the namespace prefix does not need to be embedded in forms and links.

Prerequites

Java at least 8, Struts 2.5, commons-fileupload, commons-io, Java Servlet, Gradle 6.5.1, Maven 3.6.3, Tomcat Server 9.0.24

Project Setup

You can create gradle or maven based project in your favorite IDE. The name of the project is struts-2-multiple-files-upload.

You can use build.gradle script for your gradle based project:

plugins {
	id 'war'
    id 'java-library'
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    mavenCentral()
}

dependencies {
	implementation("org.apache.struts:struts2-core:2.5.22")
	implementation 'javax.servlet:javax.servlet-api:4.0.1'
	implementation 'org.apache.logging.log4j:log4j-core:2.13.3'
	implementation 'org.apache.logging.log4j:log4j-api:2.13.3'
}

You can use below pom.xml file for your maven based project:

<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>com.roytuts</groupId>
	<artifactId>struts-2-multiple-namespaces</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>at least 1.8</java.version>
	</properties>
	
	<dependencies>		
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.5.22</version>
		</dependency>
		
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.13.3</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.13.3</version>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
                <configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Namespace Mapping

Look at the below picture how namespace in the URL is mapped.

struts 2 multiple namespaces

Action Class

You need to have only one action class for multiple namespaces. You just need to map them correctly to the same action class.

package com.roytuts.struts.multiple.namespaces;

import com.opensymphony.xwork2.ActionSupport;

public class HomeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

}

Namespace Configurations

Namespace is configured in the struts.xml file, which is kept under classpath folder src/main/resources. The below file shows there are three namespaces – “/” or root, common and user.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
<struts>
	<package name="default" namespace="/" extends="struts-default">
		<action name="home" class="com.roytuts.struts.multiple.namespaces.HomeAction">
			<result>jsp/index.jsp</result>
		</action>
	</package>
	
	<package name="common" namespace="/common"
		extends="struts-default">
		<action name="home" class="com.roytuts.struts.multiple.namespaces.HomeAction">
			<result>jsp/index.jsp</result>
		</action>
	</package>
	
	<package name="user" namespace="/user" extends="struts-default">
		<action name="home" class="com.roytuts.struts.multiple.namespaces.HomeAction">
			<result>jsp/index.jsp</result>
		</action>
	</package>
</struts>

Pictorial representation in the project structure:

struts 2 multiple namespaces

View Files

Now I am going to create index.jsp file under different namespaces. The following file is kept under webapp folder. struts-2-multiple-namespaces is the context root of the project.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 Multiple Namespaces</title>
</head>
<body>
	<a href="/struts-2-multiple-namespaces/home">root or "/" namespace</a>|
	<a href="/struts-2-multiple-namespaces/common/home">common namespace</a>|
	<a href="/struts-2-multiple-namespaces/user/home">user namespace</a>
</body>
</html>

 
Below JSP pages with same file name but location is at different module. The following file is kept under webapp/jsp folder.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 multiple Namespace</title>
</head>
<body>
	<h1>Namespace - "/"</h1>
</body>
</html>

Similarly the following file is kept under webapp/common/jsp folder.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 multiple Namespace</title>
</head>
<body>
    <h1>Namespace - "common"</h1>
</body>
</html>

Same way the following file is kept under webapp/user/jsp folder.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 multiple Namespace</title>
</head>
<body>
	<h1>Namespace - "user"</h1>
</body>
</html>

Deployment Descriptor – web.xml

The following deployment descriptor file deploys the application.

<?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 Files Upload</display-name>
	
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

Testing the Application

Once your application is running you can hit the URL http://localhost:8080/struts-2-multiple-namespaces/ and you will see the following page on your browser:

struts 2 multiple namespaces

Now clicking on each link you can check different namespaces.

Source Code

Download

Thanks for reading.

Leave a Comment