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.
See this picture to understand how a URL match to Struts 2 action namespace.
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.
2. JSP View Pages
3 JSP view pages with same file name but locate at different modules.
Root – webapp/pages/welcome.jsp
<html> <head></head> <body> <h1>Struts 2 Namespace Example</h1> <h4>Welcome - namespace = "root"</h4> </body> </html>
Common module – webapp/common/pages/welcome.jsp
<html> <head></head> <body> <h1>Struts 2 Namespace Example</h1> <h4>Welcome - namespace = "common"</h4> </body> </html>
User module – webapp/user/pages/welcome.jsp
<html> <head></head> <body> <h1>Struts 2 Namespace Example</h1> <h4>Welcome - namespace = "user"</h4> </body> </html>
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.

Everythings s interesting in your website,
i still have problem using maven, why we use maven,…?
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 have checked invalid namespace against my list of valid namespaces and accordingly I could have thrown generic error which would finally result in my standard error page.
Hello sir,
Huge Thanks for providing Very nice & quality information on your site.
Keep this going on…
Wheter namespace must map to folder structure. If I make namesoace are not map to folder structure, what will be happen.
Example:
And in WebContent the folder stucture like this
WebContent
–content
—-isnamespace
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.
Hi,
Try this maven’s style example, it externalize the properties and config file, hope help.
http://www.mkyong.com/struts2/struts-2-i18n-or-localization-example/
P.S In development environment, you can put anywhere you want. But, in the end, all files should compile or copy to WEB-INF folder for deployment.