[
https://issues.apache.org/struts/browse/WW-2842?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=45461#action_45461
]
Jeromy Evans commented on WW-2842:
----------------------------------
Don implemented the NamedVariablePatternMatcher in about mid 2008 but it's
never been fully documented. Your approach using a custom ActionMapper is also
appropriate.
The NamedVariablePatternMatcher will be properly documented in the 2.1.6
release.
I created an example for another user at:
http://www.blueskyminds.com.au/url-hierarchy/
Here's the relevant description that's being incorporated in to the docs
regarding wildcards:
The PatternMatcher is used to process variables in the path of a request. It
allows an action's namespace to contain patterns that match wildcards and/or to
extract parameters from the request's path.
The default implementation supports the * and ? notation within the namespaces
(see xxx). Switching the implementation to the NamedVariablePatternMatcher
allows named parameters to be included within the namespace.
eg. for a request to "/users/23/details.action", if the action's namespace is
"/users/{userID}/", the PatternMatcher will attempt to set the action's userID
property to 23 as if this were a request parameter.
The NamedVariablePatternMatcher is setup by:
1. Declaring the new PatternMatcher bean (undefined by default); and
2. setting the struts.patternMatcher property to the new instance's name
<code>
<bean type="com.opensymphony.xwork2.util.PatternMatcher"
name="namedVariablePatternMatcher"
class="com.opensymphony.xwork2.util.NamedVariablePatternMatcher"/>
<constant name="struts.patternMatcher" value="namedVariablePatternMatcher"/>
</code>
Namespaces can be configured in struts.xml or using annotations as usual:
<code>
@Namespace{"/users/{userID}");
public class DetailsAction exends ActionSupport {
private Long userID;
public void setUserID(Long userID) {...}
}
</code>
Caution: 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).
Caution: Some tags tags not are 100% compatible with variables in the
namespace. F or 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.
Tip: 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.
> Wildcards in namespace maps url values to controller parameters
> ---------------------------------------------------------------
>
> Key: WW-2842
> URL: https://issues.apache.org/struts/browse/WW-2842
> Project: Struts 2
> Issue Type: Improvement
> Components: Plugin - REST
> Reporter: Leonard Broman
> Priority: Minor
> Fix For: Future
>
> Attachments: rest plugin convention fix.zip
>
>
> Implement wildcard functionality in namespace declaration to be able to map
> URL values to controller properties, or more appropriately, push them to the
> value stack.
> This has been discussed lightly on the user mailing list and there is a short
> notice on the jroller blog by Don Brown
> http://www.jroller.com/mrdon/entry/struts_2_rest_todos .
> Simple example:
> We may want to map a nice and clean url: GET /report/lbroman/2008/january
> To a ReportController where the values lbroman, 2008, january will be set on
> properties on the Controller.
> Using the @Namespace annotation, a namespace declaration like
> "/report/{user}/{year}/{month}" will use the wildcard mapping to map
> "lbroman", "2008", "january" to setUser(), setYear() and setMonth()
> respectively.
> Note that this overrides the default setId() for the first parameter.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.