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

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

5. Create one more folder name as two, parallel to WEB-INF folder, create welcome.jsp file and paste below content in that file.

6. In multiple namespace if we hit the URL, it will read data from different directory depending upon namespace as show below.

OUTPUT:

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.

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0