struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeromy Evans (JIRA)" <>
Subject [jira] Commented: (WW-2842) Wildcards in namespace maps url values to controller parameters
Date Mon, 12 Jan 2009 07:32:45 GMT


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:

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

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

<bean type="com.opensymphony.xwork2.util.PatternMatcher" name="namedVariablePatternMatcher"
<constant name="struts.patternMatcher" value="namedVariablePatternMatcher"/>

Namespaces can be configured in struts.xml or using annotations as usual:

public class DetailsAction exends ActionSupport {
  private Long userID;
  public void setUserID(Long userID) {...}

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

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:
>             Project: Struts 2
>          Issue Type: Improvement
>          Components: Plugin - REST
>            Reporter: Leonard Broman
>            Priority: Minor
>             Fix For: Future
>         Attachments: rest plugin convention
> 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 .
> 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.

View raw message