Namespaces in Struts 2

Tags:

Problem summary

To navigate from from a jsp in one folder to a jsp in another folder and back again.

Download war file

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:

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 node as follows:

<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:

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.

This example is done for the sake of conceptual simplicity. In reality there is no need to have the two java files Index.java and Namespaces.java as you can use a wildcard mapping. This means that you do not need an action class for every jsp that you have and so reduces code bloat. Click here for the tutorial on wildcard mappings as this tutorial was only to familiarise you with the term namespaces in Struts 2.

Comments:

This posting is very usedful. I was searching for a simple example to know about Namespace. Good job.

That really helps me much. Thanks!

nice one

Thanks, Solved my weeks problem in a minute. Highly appriciated

Quick. Good. Easy to follow.

Cool, Thanks.
Usefull 100%.

Post a Comment:

HTML Syntax: Allowed