From the documentation: Struts2's Advanced Wildcard Mappings:
Advanced Wildcards
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:
<constant name="struts.enable.SlashesInActionNames" value="true"/> <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/> <constant name="struts.patternMatcher" value="regex" />
The regular expressions can be in two forms, the simplest one is
{FIELD_NAME}
, in which case the field with theFIELD_NAME
in the action will be populated with the matched text, for example:<package name="books" extends="struts-default" namespace="/"> <action name="/{type}/content/{title}" class="example.BookAction"> <result>/books/content.jsp</result> </action> </package>
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
".
This is absolutely great, and works fine if you read those variables in a regular Action method, like execute()
.
If you try to read them from the prepare()
method, they are null, because the PrepareInterceptor
runs before the other Interceptors responsibles for setting the parameters; the usual way to resolve this issue is to use the apposite Interceptor Stack to get the parameters already populated when executing the prepare()
method...
From the documentation: ParamsPrepareParamStack
<!-- An example of the paramsPrepareParams trick. This stack
is exactly the same as the defaultStack, except that it
includes one extra interceptor before the prepare interceptor:
the params interceptor.
This is useful for when you wish to apply parameters directly
to an object that you wish to load externally (such as a DAO
or database or service layer), but can't load that object
until at least the ID parameter has been loaded. By loading
the parameters twice, you can retrieve the object in the
prepare() method, allowing the second params interceptor to
apply the values on the object. -->
This works great for parameters coming from the page, but it does not work for the parameters set by Advanced Wildcards. They are still null.
How to resolve this issue ?