Struts2 Multi namespace
This is very interesting topic where small change can load code from different directory depending on the name space defined in struts.xml
- In struts we can define multiple namespace inside struts.xml file as highlighted in below file. e.g. namespace=”/”
It will generate an URL depending upon namespace as given below.
http://localhost:8081/struts-demo/<namespace>/<struts_action>
2. We have created namespace with name as /, /one, /two.
This will require a change in the project code structure to add one and two folder inside WebContent folder as highlighted below red in color.
3. Update content of UserAction.java class file to below one:
4. Create a folder name as one, parallel to WEB-INF folder, create welcome.jsp file and paste below content in that file.
4. Create one more folder name as two, parallel to WEB-INF folder, create welcome.jsp file and paste below content in that file.
5. In multiple namespace if we hit the URL, it will read data from different directory depending upon namespace as show below.
OUTPUT:
Get Imran Khan’s stories in your inbox
Join Medium for free to get updates from this writer.
http://localhost:8081/struts-demo/user.action
Hitting the above URL will read data from root folder welcome.jsp file.
http://localhost:8081/struts-demo/one/user.action
Hitting the above URL will read data from one folder welcome.jsp file.
http://localhost:8081/struts-demo/two/user.action
Hitting the above URL will read data from two folder welcome.jsp file.
I hope you found out this article interesting and informative. Please share it with your friends to spread the knowledge.
You can follow me for upcoming blogs follow.
Thank you!









