3

How could I map a regex number class (\d) to an action?

Example:

/user/123 , /user/345,  /user/546456 ....
Roman C
  • 49,761
  • 33
  • 66
  • 176
lovespring
  • 19,051
  • 42
  • 103
  • 153

1 Answers1

1

With Advanced Wildcards:

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 the FIELD_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".

Note that if you need those parameters in prepare() method, you need a tuned up Interceptor Stack.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243