Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > ActionMapper
Added by Patrick Lightbody, last edited by Ted Husted on Feb 12, 2007  (view change)

The ActionMapper interface provides a mapping between HTTP requests and action invocation requests and vice-versa.

When given an HttpServletRequest, the ActionMapper may return null if no action invocation request matches, or it may return an ActionMapping that describes an action invocation for the framework to try.

The ActionMapper is not required to guarantee that the ActionMapping returned be a real action or otherwise ensure a valid request. Accordingly, most ActionMappers do not need to consult the Struts configuration just to determine if a request should be mapped.

Just as requests can be mapped from HTTP to an action invocation, the opposite is true as well. However, because HTTP requests (when shown in HTTP responses) must be in String form, a String is returned rather than an actual request object.

DefaultActionMapper

By default, the DefaultActionMapper is used:

Default action mapper implementation, using the standard *.[ext] (where ext usually "action") pattern. The extension is looked up from the Struts configuration key struts.action.extension.

To help with dealing with buttons and other related requirements, this mapper (and other ActionMappers, we hope) has the ability to name a button with some predefined prefix and have that button name alter the execution behaviour. The four prefixes are:

  • Method prefix - method:default
  • Action prefix - action:dashboard
  • Redirect prefix - redirect:cancel.jsp
  • Redirect-action prefix - redirect-action:cancel

In addition to these four prefixes, this mapper also understands the action naming pattern of foo!bar in either the extension form (eg: foo!bar.action) or in the prefix form (eg: action:foo!bar). This syntax tells this mapper to map to the action named foo and the method bar.

Method prefix

With method-prefix, instead of calling baz action's execute() method (by default if it isn't overriden in struts.xml to be something else), the baz action's anotherMethod() will be called. A very elegant way determine which button is clicked. Alternatively, one would have submit button set a particular value on the action when clicked, and the execute() method decides on what to do with the setted value depending on which button is clicked.

<s:form action=&quot;baz&quot;>
    <s:textfield label=&quot;Enter your name&quot; name=&quot;person.name&quot;/>
    <s:submit value=&quot;Create person&quot;/>
    <s:submit name=&quot;method:anotherMethod&quot; value=&quot;Cancel&quot;/>
</s:form>

Action prefix

With action-prefix, instead of executing baz action's execute() method (by default if it isn't overriden in struts.xml to be something else), the anotherAction action's execute() method (assuming again if it isn't overriden with something else in struts.xml) will be executed.

<s:form action=&quot;baz&quot;>
    <s:textfield label=&quot;Enter your name&quot; name=&quot;person.name&quot;/>
    <s:submit value=&quot;Create person&quot;/>
    <s:submit name=&quot;action:anotherAction&quot; value=&quot;Cancel&quot;/>
</s:form>

Redirect prefix

With redirect-prefix, instead of executing baz action's execute() method (by default it isn't overriden in struts.xml to be something else), it will get redirected to, in this case to www.google.com. Internally it uses ServletRedirectResult to do the task.

<s:form action=&quot;baz&quot;>
    <s:textfield label=&quot;Enter your name&quot; name=&quot;person.name&quot;/>
    <s:submit value=&quot;Create person&quot;/>
    <s:submit name=&quot;redirect:www.google.com&quot; value=&quot;Cancel&quot;/>
</s:form>

Redirect-action prefix

With redirect-action-prefix, instead of executing baz action's execute() method (by default it isn't overriden in struts.xml to be something else), it will get redirected to, in this case 'dashboard.action'. Internally it uses ServletRedirectResult to do the task and read off the extension from the struts.properties.

<s:form action=&quot;baz&quot;>
    <s:textfield label=&quot;Enter your name&quot; name=&quot;person.name&quot;/>
    <s:submit value=&quot;Create person&quot;/>
    <s:submit name=&quot;redirect-action:dashboard&quot; value=&quot;Cancel&quot;/>
</s:form>

Custom ActionMapper

You can define your own ActionMapper by implementing org.apache.struts2.dispatcher.mapper.ActionMapper then configuring Struts 2 to use the new class in struts.xml

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="struts" class="com.mycompany.myapp.MyActionMapper" />

Possible uses of the ActionMapper include defining your own, cleaner namespaces, such as URLs like /person/1, which would be similar to a request to /getPerson.action?personID=1 using the DefaultActionMapper.

CompositeActionMapper

A composite action mapper that is capable of delegating to a series of ActionMapper if the former failed to obtained a valid ActionMapping or uri.

It is configured through struts.properties.

For example, with the following entries in struts.properties

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="struts" 
      class="org.apache.struts2.dispatcher.mapper.CompositeActionMapper" />
<constant name="struts.mapper.composite" 
      value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper,org.apache.struts2.dispatcher.mapper.RestfulActionMapperorg.apache.struts2.dispatcher.mapper.Restful2ActionMapper" />

When {@link CompositeActionMapper#getMapping(HttpServletRequest, ConfigurationManager)} or CompositeActionMapper#getUriFromActionMapping(ActionMapping) is invoked, CompositeActionMapper would go through these ActionMappers in sequence starting from ActionMapper identified by 'struts.mapper.composite.1', followed by 'struts.mapper.composite.2' and finally 'struts.mapper.composite.3' (in this case) until either one of the ActionMapper return a valid result (not null) or it runs out of ActionMapper in which case it will just return null for both {@link CompositeActionMapper#getMapping(HttpServletRequest, ConfigurationManager)} and CompositeActionMapper#getUriFromActionMapping(ActionMapping) methods.

For example with the following in struts-*.xml :-

   <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="struts" 
      class="org.apache.struts2.dispatcher.mapper.CompositeActionMapper" />
   <constant name="struts.mapper.composite" 
      value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper,foo.bar.MyActionMapper,foo.bar.MyAnotherActionMapper" />

CompositeActionMapper will be configured with 3 ActionMapper, namely "DefaultActionMapper", "MyActionMapper" and "MyAnotherActionMapper". CompositeActionMapper would consult each of them in order described above.

ActionMapper and ActionMapping objets

The ActionMapper fetches the ActionMapping object corresponding to a given request. Essentially, the ActionMapping is a data transfer object that collects together details such as the Action class and method to execute. The mapping is utilized by the Dispatcher and various user interface components. It is customizable through struts.mapper.class entry in struts.properties.

Customize

Custom ActionMapper must implement ActionMapper interface and have a default constructor.
struts.mapper.class=foo.bar.MyCustomActionMapper
public class MyCustomActionMapper implements ActionMapper {
  public ActionMapping getMapping(HttpServletRequest request, 
                                  ConfigurationManager configManager) {
    ....
  }

  public String getUriFromActionMapping(ActionMapping mapping) { 
    ....
  }
}

See also: RestfulActionMapper

Next: Action Proxy & ActionProxy Factory