UrlRewrite Struts2 setting parameter variables

帅比萌擦擦* 提交于 2019-12-06 12:27:10

问题


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 Integer id


回答1:


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>


来源:https://stackoverflow.com/questions/5430073/urlrewrite-struts2-setting-parameter-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!