Struts 2 Namespace configuration example and explanation

Struts 2 Namespace is a new concept to handle the multiple modules by given a namespace to each module. In addition, it can used to avoid conflicts between same action names located at different modules.

Struts 2 Namespaces are the equivalent of Struts 1 multiple modules

See this picture to understand how a URL match to Struts 2 action namespace.

namespace map url

1. Namespace configuration

Let go through a Struts 2 namescape configuration example to know how it match with URL and folder.

P.S The package “name” will not affect the result, just give a meaningful name.

struts.xml


<?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>
	
<package name="default" namespace="/" extends="struts-default">
	<action name="SayWelcome">
		<result>pages/welcome.jsp</result>
	</action>
</package>
	
<package name="common" namespace="/common" extends="struts-default">
	<action name="SayWelcome">
		<result>pages/welcome.jsp</result>
	</action>
</package>
	
<package name="user" namespace="/user" extends="struts-default">
	<action name="SayWelcome">
		<result>pages/welcome.jsp</result>
	</action>
</package>
	
</struts>

Struts 2 action namespace map to folder structure.

namespace map folder

2. JSP View Pages

3 JSP view pages with same file name but locate at different modules.

Root – webapp/pages/welcome.jsp




Struts 2 Namespace Example

Welcome - namespace = "root"

Common module – webapp/common/pages/welcome.jsp




Struts 2 Namespace Example

Welcome - namespace = "common"

User module – webapp/user/pages/welcome.jsp




Struts 2 Namespace Example

Welcome - namespace = "user"

3. Mapping – How it work?

Example 1
URL : http://localhost:8080/Struts2Example/SayWelcome.action
Will match the root namespace.


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

And display the content of webapp/pages/welcome.jsp.

Example 2
URL : http://localhost:8080/Struts2Example/common/SayWelcome.action
Will match the common namespace.


<package name="common" namespace="/common" extends="struts-default">
	<action name="SayWelcome">
		<result>pages/welcome.jsp</result>
	</action>
</package>

And display the content of webapp/common/pages/welcome.jsp.

Example 3
URL : http://localhost:8080/Struts2Example/user/SayWelcome.action
Will match the user namespace.


<package name="user" namespace="/user" extends="struts-default">
	<action name="SayWelcome">
		<result>pages/welcome.jsp</result>
	</action>
</package>

And display the content of webapp/user/pages/welcome.jsp.

Reference

  1. Struts 2 Namespace Configuration Reference

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Jam
Guest
Jam

how to use regex in namespace

duke
Guest
duke

Is there a way to keep the struts-default and the json-default here

Mehdi
Guest
Mehdi

Everythings s interesting in your website,
i still have problem using maven, why we use maven,…?

free coder
Guest
free coder
Is there any way in Struts2 by which I can get list of namespaces in my App ? I want this as set or list at runtime . I need this because => I am using Struts2 RestActionMapper plugin. When there invalid namespace is specified for valid action, Struts is throwing namespace error. But I could not redirected to standard error page when this error occurs. I tried almost all options e.g. global error mapping default namespace etc . Nothing worked. So thought it would be great if I could get list of namespaces in my app, thus i could… Read more »
Mohammed Vaseem
Guest
Mohammed Vaseem

Hello sir,
Huge Thanks for providing Very nice & quality information on your site.
Keep this going on…

bluething
Guest
bluething

Wheter namespace must map to folder structure. If I make namesoace are not map to folder structure, what will be happen.
Example:

<package name="somepackage" namespace="/isnamespace" extends="hibernate-default">
		<action name="auth" method="loginChecker" class="com.sdd.web.AuthAction">
			<result name="success">themes/index.jsp</result>
			<result name="error">login.jsp</result>
		</action>
	</package>

And in WebContent the folder stucture like this
WebContent
–content
—-isnamespace

trackback
Struts 2 Namespace « JAVA

[…] Ref Link : […]

Struts2 Newbie
Guest
Struts2 Newbie

Hi,
I am just starting with struts framework, especially struts2. i had a question regarding the configuration.
Can I place the properties files and configuration files out of the web-inf folder and to a directory of my choice?
Can I place it someplace else and refer to that location/file in my struts.xml or web.xml?

Thanks – Your response will be really appreciated.

trackback
Struts 2 – Include multiple Struts configuration files
trackback
Struts – Multiple configuration files example

[…] differential it via the “config” parameter value in web.xml. In Struts 2, the “Namespace” is a more efficient way to replace this “config parameter” […]

trackback
Struts 2 Hello World Example | J2EE Web Development Tutorials

[…] namespace=”/Hello” It’s used to match the “/Hello” URL pattern. See this article – Struts 2 namespace example and explanation. […]