Home > Guides > Core Developers Guide > ActionMapper |
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.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: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
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.
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.xml. For example, with the following entries in struts.xml
When 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 CompositeActionMapper#getMapping(HttpServletRequest, ConfigurationManager)
and CompositeActionMapper#getUriFromActionMapping(ActionMapping)
methods.
For example with the following in struts.xml:
CompositeActionMapper
will be configured with 2 ActionMapper, namely "struts" which is org.apache.struts2.dispatcher.mapper.DefaultActionMapper
and "restful" which is org.apache.struts2.dispatcher.mapper.RestfulActionMapperRestfulActionMapper
. CompositeActionMapper
would consult each of them in order described above.
A prefix based action mapper that is capable of delegating to other ActionMappers based on the request's prefix
It is configured through struts.xml
For example, with the following entries in struts.properties
<constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper"/> <constant name="struts.mapper.prefixMapping" value="/communities:pseudoRestful,/communityTags:pseudoRestful,/events:pseudoRestful,/mediaList:pseudoRestful,/users:pseudoRestful,/community:struts,/communityTag:struts,/event:struts,/media:struts,/user:struts,:struts"/>When {@link PrefixBasedActionMapper#getMapping(HttpServletRequest, ConfigurationManager)} or PrefixBasedActionMapper#getUriFromActionMapping(ActionMapping) is invoked, PrefixBasedActionMapper will check each possible prefix (url prefix terminating just before a /) to find the most specific ActionMapper that returns a mapping when asked to map the request. If none are found, null is returned for both {@link PrefixBasedActionMapper#getMapping(HttpServletRequest, ConfigurationManager)} and PrefixBasedActionMapper#getUriFromActionMapping(ActionMapping) methods.
Prefix based factory should be used with org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper to use appropriate com.opensymphony.xwork2.ActionProxyFactory connected with given org.apache.struts2.dispatcher.mapper.ActionMapper
Add below entry to struts.xml to enable the factory:
<constant name="struts.actionProxyFactory" value="prefix"/>
The factory will use the same set of patterns as defined with:
<constant name="struts.mapper.prefixMapping" value="..."/>
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
or struts.xml
. Note that the value of this constant is the name of the bean of the new mapper.
See also: RestfulActionMapper