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.
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 -
After clicking on submit button, the default namespace will be called, and it will display -
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
For course two, the URL might look something like this -
http://localhost:8080/multiple_namespace/ctwo/click
The General syntax for namespace -
http://host_name:port_no/WebApp_name/namespace_name/action_name
Default namespace syntax -