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

<html>
<head></head>
<body>
<h1>Struts 2 Namespace Example</h1>
 
<h2>Welcome - namespace = "root"</h2>
 
</body>
</html>

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

<html>
<head></head>
<body>
<h1>Struts 2 Namespace Example</h1>
 
<h2>Welcome - namespace = "common"</h2>
 
</body>
</html>

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

<html>
<head></head>
<body>
<h1>Struts 2 Namespace Example</h1>
 
<h2>Welcome - namespace = "user"</h2>
 
</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.

Reference

  1. Struts 2 Namespace Configuration Reference
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.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

  • Pingback: watch tv show episodes()

  • Pingback: watch free movies online()

  • Pingback: watch movies online free()

  • Pingback: watch movies online()

  • Pingback: Blue Coaster33()

  • Mehdi

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

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

  • Mohammed Vaseem

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

  • http://itfromzerotohero.wordpress.com/ bluething

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

    &lt;package name=&quot;somepackage&quot; namespace=&quot;/isnamespace&quot; extends=&quot;hibernate-default&quot;&gt;
    		&lt;action name=&quot;auth&quot; method=&quot;loginChecker&quot; class=&quot;com.sdd.web.AuthAction&quot;&gt;
    			&lt;result name=&quot;success&quot;&gt;themes/index.jsp&lt;/result&gt;
    			&lt;result name=&quot;error&quot;&gt;login.jsp&lt;/result&gt;
    		&lt;/action&gt;
    	&lt;/package&gt;

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

  • Pingback: Struts 2 Namespace « JAVA()

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

    • http://www.mkyong.com mkyong

      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.

  • Pingback: Struts 2 – Include multiple Struts configuration files()

  • Pingback: Struts – Multiple configuration files example()

  • Pingback: Struts 2 Hello World Example | J2EE Web Development Tutorials()