Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wildcards

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.

...

Also, the action mapping and action result properties (set using the <set-property key="foo" value="bar"> syntax) will accept wildcard-matched strings in their value attribute.

(lightbulb) See also Wildcard Method

Parameters in namespaces

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:

Code Block
xml
xml

<constant name="struts.patternMatcher" value="named-variable"/>

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:

Code Block
java
java

@Namespace{"/users/{userID}");
public class DetailsAction exends ActionSupport {
  private Long userID;
  public void setUserID(Long userID) {...}
}

If the request URL is /users/10/detail, then the DetailsAction will be executed and its userID field will be set to 10.

Warning
Wiki Markup

Only one PatternMatcher implementation can be used at a time.  The two implementations included with Struts 2 are mutually exclusive.  You cannot use Wildcards and Named Variable patterns at the same application (if that were required, you'd need to create a custom PatternMatcher implementation).

Some tags tags not are 100% compatible with variables in the namespace. For instance, they may write the literal namespace into the HTML (eg /{user}/2w) instead of the path used in the request (ie. /brett/24).  This usually affects attributes that attempt to guess the namespace of an action (eg. Form tag, Action tag, action=). This problem can be avoided by using HTML tags directly with relative paths or explicit URLs.
Note

Similar functionality can also be implemented using a custom ActionMapper. The ActionMapper will need to parse the namespace and request itself to set parameters on the matched action. The default ActonMapper is responsible for invoking the PatternMatcher.

Next: Application Servers