Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction🤔
2.
Understanding the Needs🎯
2.1.
Example🧐
3.
Frequently Asked Questions
3.1.
What is the Struts framework?
3.2.
What is Web Framework?
3.3.
What is API?
3.4.
Why are multiple namespaces used?
3.5.
What is 'package' in Struts XML?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Multi namespace

Author Sanchit Kumar
0 upvote

Introduction🤔

Sometimes, while using the Struts web application framework, one might wonder how to handle the same action names for multiple modules. The answer is with the use of multiple namespaces.

Struts

So let us learn about multiple namespaces to separate the JSP pages for the same actions.

Understanding the Needs🎯

Consider a scenario where managers and employees input their respective credentials on the login page. Here we have a 'login' action, and all users will be directed to the same login page (JSP-page). However, depending on the credentials entered, the page will be forwarded to different users' JSPs. So managers and employees will have the same login page and action (in the login form), but depending on the authentication, they will be led to different home pages. Therefore to handle these types of situations, we need multiple namespaces.

Example🧐

In this example, we will build a basic web application that demonstrates the use of multiple namespaces. The application uses a submit button that has action and its mapping in struts.xml.
 

Step1💼 - Setup basic struts2 web application and name it as multiple_namespace.
 

Step2✍️ - Create a "jsp" folder in the "webapp" directory. All of our JSP pages will be located here. Inside the folder, create the first JSP page 'first.jsp' that will initiate the web application and another JSP page 'next.jsp' for redirection on button click. This 'next.jsp' will be used for the default namespace in our WebApp.

first.jsp

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
   <head>
       <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
       <title>JSP Page</title>
   </head>
   <body>        
       <s:form action = "click">
                   Click here to view courses
           <s:submit value = "SUBMIT"/>
       </s:form>
  </body>
</html>


next.jsp

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
   <head>
       <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
       <title>JSP Page</title>
   </head>
   <body>        
       <h1>NEXT PAGE</h1>
  </body>
</html>


Step3✍️ - Now, create two folders inside the "webapp" folder along with the "jsp" folder to redirect action to different JSPs in different folders. Name these folders "student_cone" and "student_ctwo". Create cone.jsp file and ctwo.jsp file inside folder "student_cone"and "student_ctwo", respectively.

cone.jsp

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
   <head>
       <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
       <title>JSP Page</title>
   </head>
   <body>
   <!-- for multiple namespaces config 1-->
       <h1>Welcome to Course 1</h1>
   </body>
</html>


ctwo.jsp

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
   <head>
       <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
       <title>JSP Page</title>
   </head>
   <body>
   <!-- for multiple namespaces config 2-->
       <h1>Welcome to Course 2</h1>
   </body>
</html>


Step4🧶 - Create the action class.

course.java

package action_class;
import com.opensymphony.xwork2.ActionSupport;
public class course extends ActionSupport {
   public String exec()
   {
       return "success";
   }
}


Step5🌪 - Namespace Configuration

By using the namespace attribute of the package element, we can declare multiple namespaces in the struts.xml file.

Write the following code in the struts.xml file - 

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts> 

  <!-- Configuration for -> default package. -->
  <package name = "default" extends = "struts-default" namespace = "/">
       <action name = "click" class = "action_class.course" method = "exec">
           <result name = "success"> jsp/next.jsp </result>
       </action>    
   </package>
   <!-- multiple namespaces config 1-->
   <package name = "student_cone" extends = "struts-default" namespace = "/cone">
       <action name = "click" class = "action_class.course" method = "exec">
           <result name = "success"> course_cone.jsp </result>
       </action>
   </package>
   <!-- multiple namespaces config 2-->
   <package name = "student_ctwo" extends = "struts-default" namespace = "/ctwo">
       <action name = "click" class = "action_class.course" method = "exec">
           <result name = "success"> course_ctwo.jsp </result>
       </action>
   </package>
</struts>


Step6🕸 - Make changes to the web.xml file.

web.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<web-app version = "3.1" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
   <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>
   <session-config>
       <session-timeout>
           31
       </session-timeout>
   </session-config>
   <welcome-file-list>
       <welcome-file> jsp/first.jsp </welcome-file>
   </welcome-file-list>
</web-app>


Step7 - Execution🦾

On the first run, the web application will display -

Click here to view courses

After clicking on submit button, the default namespace will be called, and it will display -

NEXT PAGE

However, in order to jump to the course page, a user must call the appropriate namespace, which is accomplished using URL rewriting.

Example - 

For course one, the URL might look something like this -

http://localhost:8080/multiple_namespace/cone/click 

Welcome to Course 1

 

For course two, the URL might look something like this -

http://localhost:8080/multiple_namespace/ctwo/click 

Welcome to Course 2

 

The General syntax for namespace -

http://host_name:port_no/WebApp_name/namespace_name/action_name 

Default namespace syntax -

http://host_name:port_no/WebApp_name/action_name 

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

Why are multiple namespaces used?

Multiple namespaces are used to handle the same action names for multiple modules.

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.

Conclusion

In this article, we discussed why we need multiple namespaces and its use in the struts2 web application framework with an example.

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