Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > ActionMapper
Added by plightbo, last edited by Ted Husted on Sep 03, 2006  (view change)
Content pulled from external source. Click here to refresh.

The ActionMapper is responsible for providing 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 maps, or it may return an ActionMapping that describes an action invocation that Struts should attempt to try. The ActionMapper is not required to guarantee that the ActionMapping returned be a real action or otherwise ensure a valid request. This means that most ActionMappers do not need to consult the Struts configuration 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:

Content pulled from external source. Click here to refresh.

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.exection.

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

Content pulled from external source. Click here to refresh.

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.

Content pulled from external source. Click here to refresh.
<a:form action=&quot;baz&quot;>
    <a:textfield label=&quot;Enter your name&quot; name=&quot;person.name&quot;/>
    <a:submit value=&quot;Create person&quot;/>
    <a:submit name=&quot;method:anotherMethod&quot; value=&quot;Cancel&quot;/>
</a:form>

Action prefix

Content pulled from external source. Click here to refresh.

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.

Content pulled from external source. Click here to refresh.
<a:form action=&quot;baz&quot;>
    <a:textfield label=&quot;Enter your name&quot; name=&quot;person.name&quot;/>
    <a:submit value=&quot;Create person&quot;/>
    <a:submit name=&quot;action:anotherAction&quot; value=&quot;Cancel&quot;/>
</a:form>

Redirect prefix

Content pulled from external source. Click here to refresh.

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.

Content pulled from external source. Click here to refresh.
<a:form action=&quot;baz&quot;>
    <a:textfield label=&quot;Enter your name&quot; name=&quot;person.name&quot;/>
    <a:submit value=&quot;Create person&quot;/>
    <a:submit name=&quot;redirect:www.google.com&quot; value=&quot;Cancel&quot;/>
</a:form>

Redirect-action prefix

Content pulled from external source. Click here to refresh.

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.

Content pulled from external source. Click here to refresh.
<a:form action=&quot;baz&quot;>
    <a:textfield label=&quot;Enter your name&quot; name=&quot;person.name&quot;/>
    <a:submit value=&quot;Create person&quot;/>
    <a:submit name=&quot;redirect-action:dashboard&quot; value=&quot;Cancel&quot;/>
</a:form>

ActionMapperFactory

You can define your own ActionMapper by configuring the ActionMapperFactory:

Content pulled from external source. Click here to refresh.

Factory that creates ActionMappers. This factory looks up the class name of the ActionMapper from Struts's configuration using the key struts.mapper.class.

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.

Next: test:Dependency Injection