You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

RESTful URLs are experimental. Feeback appreciated!

RestfulActionMapper

Error formatting macro: snippet: java.lang.NullPointerException

Restful2ActionMapper

Error formatting macro: snippet: java.lang.NullPointerException

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

<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

<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

<constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
<constant name="struts.mapper.prefixMapping" value="/rest:restful2,:struts" />

For the Restful2ActionMapper to work we also have to set

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