Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > ActionMapper > RestfulActionMapper
Added by Ted Husted, last edited by Ted Husted on Feb 12, 2007  (view change)
RESTful URLs are experimental. Feeback appreciated!

RestfulActionMapper

A custom action mapper using the following format:

    http://HOST/ACTION_NAME/PARAM_NAME1/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2

You can have as many parameters you'd like to use. Alternatively the URL can be shortened to the following:

    http://HOST/ACTION_NAME/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2

This is the same as:

    http://HOST/ACTION_NAME/ACTION_NAME + "Id"/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2

Suppose for example we would like to display some articles by id at using the following URL sheme:

    http://HOST/article/Id

Your action just needs a setArticleId() method, and requests such as /article/1, /article/2, etc will all map to that URL pattern.

Restful2ActionMapper

Improved restful action mapper that adds several ReST-style improvements to action mapping, but supports fully-customized URL's via XML. The two primary ReST enhancements are:

  • If the method is not specified (via '!' or 'method:' prefix), the method is "guessed" at using ReST-style conventions that examine the URL and the HTTP method.
  • Parameters are extracted from the action name, if parameter name/value pairs are specified using PARAM_NAME/PARAM_VALUE syntax.

These two improvements allow a GET request for 'category/action/movie/Thrillers' to be mapped to the action name 'movie' with an id of 'Thrillers' with an extra parameter named 'category' with a value of 'action'. A single action mapping can then handle all CRUD operations using wildcards, e.g.

  <action name="movie/*" className="app.MovieAction">
    <param name="id">{0}</param>
    ...
  </action>

The following URL's will invoke its methods:

  • GET: /movie => method="index"
  • GET: /movie/Thrillers => method="view", id="Thrillers"
  • GET: /movie/Thrillers!edit => method="edit", id="Thrillers"
  • GET: /movie/new => method="editNew"
  • POST: /movie/Thrillers => method="create"
  • PUT: /movie/ => method="update"
  • DELETE: /movie/Thrillers => method="remove"

To simulate the HTTP methods PUT and DELETE, since they aren't supported by HTML, the HTTP parameter "__http_method" will be used.

The syntax and design for this feature was inspired by the ReST support in Ruby on Rails. See http://ryandaigle.com/articles/2006/08/01/whats-new-in-edge-rails-simply-restful-support-and-how-to-use-it