javareststruts2struts2-rest-plugin

Should the namespace (and action) start with a slash?


We are developing an Struts 2 base web application.

Please let me know which is correct (standard) in Strust 2 application

<s:url var="url" action="foo" namespace="bar" />
<s:url var="url" action="/foo" namespace="/bar" />
<s:url var="url" action="foo" namespace="/bar" />

Long story:

To get an action url we always used below

<s:url var="url" action="foo" namespace="bar" />

We tried to add the rest support to our application and make our application support rest and non rest actions same as mentioned in REST and non-RESTful URL's Together Configuration

and also Struts2 REST and Non-REST action together.

It seems that the <s:url var="url" action="foo" namespace="bar" /> is always mapped to default action, while the <s:url var="url" action="/foo" namespace="/bar" /> works fine.

Is it a bug in org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper class or the action and namespace should always have slashes?


Solution

  • Below the line

    REST and non-RESTful URL's Together Configuration

    And, again, we're relying on the Convention plugin to find our controllers, so we need to configure the convention plugin a bit:

    <constant name="struts.convention.action.suffix" value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <constant name="struts.convention.default.parent.package" value="rest-default"/>
    <constant name="struts.convention.package.locators" value="example"/>
    

    says that you should use Convention plugin, that means convention based action mapper. This mapper allows the action name with a leading slash. You are using Struts for years and should know that default action mapper doesn't.

    The s:url tag doesn't define any namespace or action in the configuration, it's using UrlHelper to generate url based on its own logic. What is generated is used to access the action mapper. Normally URLs are generated with

     http://<host>:<port>/<context>/<namespace>/<action>[.<extension>]
    

    If you print the url generated from the tags you'll see which is correct one. We are using namespace with a leading slash and action name without slash in the action attribute. These values should correspond to the configuration settings.