Pages Navigation Menu

Coding is much easier than you think

Struts 2 Namespace Configuration

Struts 2 Namespace Configuration

 
Struts 2 Namespace is a new concept to handle the multiple modules by giving a namespace to each module
 

  • The namespace attribute of element in struts.xml is an optional attribute. The default namespace is “” . i.e. an empty string.
  • The value of your namespace must always begin with a “/”.
  • Namespaces are used to group together a set of actions.
  • An action with the same name can exist in several namespaces. The action that will be invoked depends upon the namespace in which it is called.

 
** UPDATE: Struts 2 Complete tutorial now available here.
 
See this picture to understand how an URL matches to Struts 2 action namespace.
 
namespace
 

1. Final project structure

 
Namespace struts 2 structure
 

2. Namespace configuration

 
Let us go through a Struts 2 namespace configuration example to know how it matches with URL and folder.
 

Note :

The package “€œname”€ will not affect the result, just give a meaningful name.


 
File:struts.xml
 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	
	<package name="default" namespace="/" extends="struts-default">
		<action name="default">
			<result>jsp/home.jsp</result>
		</action>
	</package>

	<package name="staff" namespace="/staff" extends="struts-default">
		<action name="Staff">
			<result>/jsp/staff.jsp</result>
		</action>
	</package>

	<package name="student" namespace="/student" extends="struts-default">
		<action name="Student">
			<result>/jsp/student.jsp</result>
		</action>
	</package>

</struts>

 

3. JSP Pages

 
File:home.jsp
 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h2> Welcome user</h2>
</body>
</html>

 
File:Staff.jsp
 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Staff home page</title>
</head>
<body>
<h2>Welcome staff</h2>
</body>
</html>

 

File:Student.jsp
 


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>

<head>
<title>Student home page</title>
</head>
<body>
<h2>Welcome Student</h2>
</body>

</html>

 

4. Mapping -€“ How it works?

 
Example 1
 

The URL : http://localhost:8089/Name_Space/student/Student.action

.
Will match the root namespace, and display the content of WebContent/jsp/student.jsp.

<package name="student" namespace="/student" extends="struts-default">
<action name="Student">
	<result>/jsp/student.jsp</result>
</action>
</package>

 

Example 2
 

URL : http://localhost:8089/Name_Space/staff/Staff.action

Will match the common namespace, and display the content of WebContent/jsp/staff.jsp

<package name="staff" namespace="/staff" extends="struts-default">
<action name="Staff">
	<result>/jsp/staff.jsp</result>
</action>
</package>

 
Example 3
 

URL : http://localhost:8089/Name_Space/default 

Will match the common namespace, and display the content of WebContent/jsp/home.jsp.

<package name="default" namespace="/" extends="struts-default">
	<action name="default">
		<result>jsp/home.jsp</result>
	</action>
</package>

 
** UPDATE: Struts 2 Complete tutorial now available here.
 

Note :

Given below is the genaralised url

 Http://<server>:<port>/<webapp>/<namespace>/<action>.action

 
download
 

About Mohaideen Jamil