Multiple namespaces example in Struts2

I will show you here how to create multiple namespaces in struts2 web application. The benefit of namespace is the same file and action can be mapped to the multiple modules.

First create the web application using any JEE based IDE like Eclipse.
Make sure that the following jars exist in the WB-INF/lib directory. Please download the following jar files from the respective site.

file upload example in struts
We can define multiple namespaces in struts.xml file by the namespace attribute of package element. The default namespace is /(root).

Look at the below picture how namespace in the URL is mapped.

namespace example in struts

1. Creating Action class

 

2. Namespace Configurations

Namespace is configured in the struts.xml file. The below file shows there are three namespaces – “/” or root, common and user.

 

namespace example in struts
3. JSP Pages

index.jsp

 

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

jsp/index.jsp

 

common/jsp/index.jsp

 

user/jsp/index.jsp

 

4. Mapping

http://localhost:8080/struts2multiplenamespace/home

 

http://localhost:8080/struts2multiplenamespace/common/home

 

http://localhost:8080/struts2multiplenamespace/user/home

 

Once you run the application you will see the following page. Now you can navigae to any of the namespaces.
namespace example in struts

Thanks for your patience. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.