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:
<constant name="struts.patternMatcher" value="namedVariable"/>
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:
@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.
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. |
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. |