Namespaces in Struts 2
Problem summary
To navigate from from a jsp in one folder to a jsp in another folder and back again.Most people probably don’t have a problem getting their heads around namespaces in Struts 2 but I did so I thought a short explanation of how they work was in order.
To think of Struts 2 namespaces as folders when you start out can be helpful if you are not used to the term namespaces:
- The homepage will generally have namespace of / in a simple application;
- If you have a folder at the root of your application called namespacesFolder then the namespace for that folder is /namespacesFolder.
Any pages at the root of your application (in the same place as your home page) will also have the namespace of / and any pages in the folder /namespacesFolder will have the namespace of /namespacesFolder. So in your struts.xml file you will have a
<package name="root" extends="struts-default" namespace="/">
<action name="indexAction" class="struts2you.examplenamespaces.Index">
<result name="success">index.jsp</result>
</action>
</package>
<package name="namespacesFolder" extends="struts-default" namespace="/namespacesFolder">
<action name="namespacesAction" class="struts2you.examplenamespaces.Namespaces">
<result name="success">/namespacesFolder/namespaces.jsp</result>
</action>
</package>
The package named root exists in the namespace / so if you have an index.jsp/index.jsp just as you would expect or /index.action if you need struts to process things first. The result of hitting /index.action is to redirect you to /index.jsp.
The package named namespacesFolder exists in the namespace /namespacesFolder so if you have a namespaces.jsp page in the /namespacesFolder folder, you would be able to access it by using /namespacesFolder/namespaces.jsp just as you would expect or /namespacesFolder/namespaces.action if you need struts to process things first. The result of hitting /namespacesFolder/namespaces.action is to redirect you to /namespacesFolder/namespaces.jsp but you will need to write the Namespaces.java file first.
In order for this to work we need two very simple classes:
- A struts2you.examplenamespaces.Index class;
- A struts2you.examplenamespaces.Namespaces class
Their purpose is simple to return the String SUCCESS to Struts so that Struts will then re-direct them to the correct page:
package struts2you.examplenamespaces;
import com.opensymphony.xwork2.ActionSupport;
public class Index extends ActionSupport {
public String execute(){
return SUCCESS;
}
}
and:
package struts2you.examplenamespaces;
import com.opensymphony.xwork2.ActionSupport;
public class Namespaces extends ActionSupport {
public String execute(){
return SUCCESS;
}
}
When the String SUCCESS is returned then Struts will look in the struts.xml file and find the class executing the action. It will then find the result node with the value SUCCESS and send the user to the value of that node. In this case for the the action namespacesAction we will be sent to /namespacesFolder/namespaces.jsp
There you are, finished.
17.02.2009 11:17 - Posted by doahh - Comments: 6 - Java

Comments:
This posting is very usedful. I was searching for a simple example to know about Namespace. Good job.
09.12.2009 10:11 - Posted by Dhanya - Permalink
That really helps me much. Thanks!
07.04.2010 04:16 - Posted by Unknown - Permalink
nice one
01.02.2011 12:59 - Posted by vamsi - Permalink
Thanks, Solved my weeks problem in a minute. Highly appriciated
13.02.2011 08:27 - Posted by amit - Permalink
Quick. Good. Easy to follow.
17.03.2011 12:29 - Posted by kurinchiblogger - Permalink
Cool, Thanks.
Usefull 100%.
11.12.2011 05:58 - Posted by Alex - Permalink