javaurl-rewritingstruts2tuckey-urlrewrite-filter

Setting parameter variables using UrlRewrite in Struts 2


I'm using Tuckey UrlRewrite in combination with a Struts2 application.

I'm trying to convert following URL:

/promotions/abcdef-987 to /dopromotions/detail passing variable id as 987.

My rewrite rule is as follows:

<rule>
    <from>^/(promoties|promotions)/([0-9a-zA-Z\-_]+)-([0-9]+)$</from>
    <set type="parameter" name="id">$3</set>
    <to>/dopromotions/detail</to>
</rule>

And my Struts2 Action has following getters and setters:

private Integer id;
public void setId(Integer id){
    this.id = id;
}
public Integer getId(Integer id){
    return id;
}

However, the variable never gets entered. When debugging, I can't find id anywhere in the parameter or attribute scope.

I've tried removing type="parameter". This puts id in the attribute scope, but it doesn't get entered in my variable Integer id.


Solution

  • I'm not familiar with the URL re-writer you are using, but you can achieve this sort of mapping with Struts2 alone.

    Please refer to this answer about the NamedVariablePatternMatcher. You'll need the following constants set in your struts.xml:

    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
    <constant name="struts.enable.SlashesInActionNames" value="true"/>
    <constant name="struts.patternMatcher" value="namedVariable"/>
    

    Then, map your action as:

    <!-- you could also make /promotions a namespace and the action just "abcdef-{id}" -->
    <action name="promotions/abcdef-{id}" class="...">
        ...
    </action>