Home > Guides > Core Developers Guide > Configuration Elements > Wildcard Mappings |
As an application grows in size, so will the number of action mappings. Wildcards can be used to combine similar mappings into one more generic mapping.
The best way to explain wildcards is to show an example and walk through how it works. This example modifies a conventional mapping to use wildcards to match all pages that start with /edit:
The "*" in the name attribute allows the mapping to match the request URIs /editSubscription, editRegistration, or any other URI that starts with /edit, however /editSubscription/add would not be matched. The part of the URI matched by the wildcard will then be substituted into various attributes of the action mapping and its action results replacing {1}. For the rest of the request, the framework will see the action mapping and its action results containing the new values.
Mappings are matched against the request in the order they appear in the framework's configuration file. If more than one pattern matches the last one wins, so less specific patterns must appear before more specific ones. However, if the request URL can be matched against a path without any wildcards in it, no wildcard matching is performed and order is not important. Also, note that wildcards are not greedy, meaning they only match until the first occurrence of the following string pattern. For example, consider the following mapping:
This mapping would work correctly for the URI ListAccounts
but not ListSponsors
, because the latter would turn into this configuration:
Wildcard patterns can contain one or more of the following special tokens:
* | Matches zero or more characters excluding the slash ('/') character. |
** | Matches zero or more characters including the slash ('/') character. |
\character | The backslash character is used as an escape sequence. Thus '\*' matches the character asterisk ('*'), and '\\' matches the character backslash ('\'). |
In the action mapping and action results, the wildcard-matched values can be accessed with the token {N} where N is a number from 1 to 9 indicating which wildcard-matched value to substitute. The whole request URI can be accessed with the {0} token.
Also, the action mapping and action result properties will accept wildcard-matched strings in their value attribute, like:
See also Wildcard Method
From Struts 2.1+ namespace patterns can be extracted as request parameters and bound to the action. To enable this feature, set the following constant in struts.xml:
With that in place, namespace definitions can contain {PARAM_NAME} patterns which will be evaluated against the request URL and extracted as parameters, for example:
If the request URL is /users/10/detail, then the DetailsAction will be executed and its userID field will be set to 10.
To use parameters in the URL, after the action name, make sure this is set:
Then the action mapping will look like:
When a URL like /edit/person/123
is requested, EditAction will be called, and its "id" field will be set to 123.
From 2.1.9+ regular expressions can be defined defined in the action name. To use this form of wild card, the following constants must be set:
The regular expressions can be in two forms, the simplest one is {FIELD_NAME}, in which case the field with the FIELD_NAME in the action will be populated with the matched text, for example:
In this example, if the url /fiction/content/Frankenstein
is requested, BookAction's field "type" will be set to "fiction", and the field "title" will be set to "Frankenstein".
The regular expression can also be in the form {FIELD_NAME:REGULAR_EXPRESSION}. The regular expression is a normal Java regular expression. For example:
In this example, if the url /philosophy/AynRand/list
is requested, ListBooksAction's field "type" will be set to "philosophy" and "author" to "AynRand".
The matched groups can still be accessed using the {X} notation, like: