k_rollo
k_rollo

Reputation: 5472

Is prefix needed for Struts2 namespace in forms and links?

From the Apache documentation:

While the prefix appears in the browser URI, the tags are "namespace aware", so the namespace prefix does not need to be embedded in forms and links.

struts.xml:

<package name="testpkg" namespace="/test" extends="struts-default">             
    <action name="doTest" class="otes.test.TestAction">
        <result>/success.jsp</result>
    </action>       
</package>

index.jsp: (http://localhost:8080/nsdemo/)

<h2>Using HTML tags:</h2>
<h3><a href="doTest">doTest without namespace</a></h3> <!-- 404 error -->
<h3><a href="test/doTest">doTest with namespace</a></h3> <!-- works -->

<h2>Using Struts2 tags:</h2>
<h3><s:a href="doTest">doTest without namespace (s:a href)</s:a></h3> <!-- 404 error -->
<h3><s:a href="test/doTest">doTest with namespace (s:a href)</s:a></h3> <!-- works -->

<!-- 404 error -->
<s:url action="doTest" var="myAction" />
<h3><s:a href="%{myAction}">doTest without namespace (s:url action)</s:a></h3>

<!-- works -->
<s:url action="test/doTest" var="myAction" />
<h3><s:a href="%{myAction}">doTest with namespace (s:url action)</s:a></h3>

Does this mean I really have to specify the namespace in my forms and links?

(I am using Struts 2.3.20 if that matters.)

Upvotes: 1

Views: 1262

Answers (2)

Aleksandr M
Aleksandr M

Reputation: 24396

First of all: the tags referred in the docs are Struts2 tags (e.g. <s:url>, <s:a>).

And namespace aware means that if you already executed some action in particular namespace then in the JSP you don't need to prefix S2 links and forms with the current namespace.

E.g. if you have this package configuration:

<package name="testpkg" namespace="/test" extends="struts-default">             
    <action name="index">/index.jsp</action>
    <action name="doTest" class="otes.test.TestAction">
        <result>/success.jsp</result>
    </action>       
</package>

and executed the index action (http://localhost/app/test/index.action). Then in index.jsp you can write

<s:a action="doTest">test</s:a>

and that url will take you to the same namespace.

If you want to change namespace there is namespace attribute in some tags that you can use.

E.g. you are in some page (http://localhost/app/index.action) - note no namespace in url, then following link will execute doTest in /test namespace.

<s:a action="doTest" namespace="/test">test</s:a>

BTW don't add action extension to actions in S2 tags.

This is wrong:

<s:form action="doTest.action"> 

This is correct:

<s:form action="doTest">

Upvotes: 1

Roman C
Roman C

Reputation: 1

The rule is simple: if you defined a package with the namespace attribute then its actions belong to this namespace. And when you use url or a, or form tags you should specify a namespace attribute along with action name which is in action attribute. If you specify both parameters Struts can easy map your URL to the action from the package with namespace. Don't use action extension in these attributes. Struts is using UrlHelper class to build the url and if it can't find the action mapping it will return the string as is. Also if you are using href attribute then Url helper is not involved, so the string remains as is.

This code should work:

<s:url namespace="/test" action="doTest" var="myAction" />
<h2><s:a href="%{#myAction}">doTest with href (s:url action)</s:a></h2>
<h2><s:a namespace="/test" action="doTest">doTest with namespace and action (s:url action)</s:a></h2>

Upvotes: 1

Related Questions