I am still confused how to properly use the namespace attribute in Struts2.
In the namespace configuration, it was mentioned that:
Namespaces are not a path!
Namespace are not hierarchical like a file system path. There is one namespace level. For example if the URL /barspace/myspace/bar.action is requested, the framework will first look for namespace /barspace/myspace. If the action does not exist at /barspace/myspace, the search will immediately fall back to the default namespace "". The framework will not parse the namespace into a series of "folders". In the Namespace Example, the bar action in the default namespace would be selected.
I have tried making a simple Struts2 sample:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="defaultIndex">
<result name="success">/pages/default.jsp</result>
</action>
</package>
<package name="package1" namespace="/" extends="struts-default">
<action name="index1">
<result name="success">/pages/home1.jsp</result>
</action>
</package>
<package name="package2" namespace="/namespace1" extends="struts-default">
<action name="index2">
<result name="success">/pages/home2.jsp</result>
</action>
</package>
<package name="package3" namespace="/namespace1/namespace2" extends="struts-default">
<action name="index3">
<result name="success">/pages/home3.jsp</result>
</action>
</package>
</struts>
Where SampleDynamicWebProject
is the context root.
Based on the documentation, if I try
.../SampleDynamicWebProject/randomText/defaultIndex
Then, Struts2 will look for the /randomText
namespace and check for the defaultIndex
action. If it doesn't exist, then it will look for the default namespace which is the package with no namespace attribute.
But if I try this URL:
.../SampleDynamicWebProject/namespace1/randomText/index2
Struts2 should look at the /namespace1/randomText
namespace for the index2
action and if it cannot see one, then it should look at the default namespace. However, the URL above is still directed at the index2
action in the /namespace1
.
The same thing is happening when I try
.../SampleDynamicWebProject/randomText/index1
The index1
in the root namespace is invoked.
Can you please clarify how exactly it works?