Struts2 Tutorial Part 5 – Namespaces

In this session we will discuss about Struts2 namespaces.Struts2 namespaces are the Namespaces of the URL accessed.

For example if a user types types a URL http://www.learnerandtutor.com/struts2/actions/method.action  in that /struts2/action  is namespace and method.action  is the action name.

While naming a namespace we have to keep the below points in mind.

  • A namespace will be like “/path1/subpath”, “/path2/path3/sub1”.
  • A namespace can also be root namespace. (i.e) namespace=”/”. But it is exactly like normal namespace. The URL should match for it.
  • For example if the url is “www.learner.com/rajesh.action” it will go to root namespace. If the url is “www.learner.com/path1/subpath” it will go to “/path1/subpath” namespace.
  • We can also create default namespace where we can put default actions. That means if the requested action is not found in any specific namespaces then the control will look for that action inside the default namespace. The default namespace syntax will be
<package name="serveAll" namespace="" extends="struts-default">
  .
  .
  .
</package>

One useful example of this default is we can handle all the unknown requests with the the simple setup like below.

<package name="serveAll" namespace="" extends="struts-default">
        <action name="*">
            <result>/error.jsp</result>
        </action>
</package>

What we done is, if any requested action is not found simply responding with error page instead of file not found error. That’s why we put the action name as *. So even if the user types an action name asdasdasd it will respond with error.jsp . Even we can use wild card characters also in action name. In later chapters we will work on wild char characters in action names.

One Comment

Add a Comment

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