Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
{snippet:id=description|javadoc=true|url=org.apache.struts2.dispatcher.mapper.Restful2ActionMapper}

Example

To use the Restful2ActionMapper in an existing struts application we have to change the strus.mapper.class constant and let it point to the Restful2ActionMapper

Code Block
xml
xml

<constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.Restful2ActionMapper" />

The problem with the above approach is that we may break existing actions because the Restful2ActionMapper tries to guess the method name using conventions that aren't applicable to normal action classes.

To overcome the above problem, we have to use a different action mapper depending on the url we want to process. REST actions will be processed by the Restful2ActionMapper and non-REST actions by the DefaultActionMapper

To achieve that we have to rely on nampespaces and the PrefixBasedActionMapper that can choose which action mapper to use for a particular url based on a prefix (the action namespace).

To put everything together, we create a package for our rest actions

Code Block
xml
xml

<package name="rest" namespace="/rest" extends="struts-default">
    ....interceptor config
    <action name="movie/*" class="app.MovieAction">
        <param name="id">{0}</param>
        ....results
    </action>
    ....
</package>

All other actions remain in their existing packages and namespaces we use the PrefixBasedActionMapper telling it to use the Restful2ActionMapper for actions in the /rest namespace and the DefaultActionMapper for all other actions

Code Block
xml
xml

<constant name="struts.mapper.class" value="
Wiki Markup
{snippet:id=example|javadoc=true|url=org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
<constant name="struts.mapper.Restful2ActionMapper}prefixMapping" value="/rest:restful2,:struts" />

For the Restful2ActionMapper to work we also have to set

Code Block
xml
xml

<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />