我将 Tuckey UrlRewrite 与 Struts2 应用程序结合使用.
我正在尝试转换以下网址:
/promotions/abcdef-987 到 /dopromotions/detail 将变量 id 作为 987 传递.>
我的重写规则如下:
<from>^/(promoties|promotions)/([0-9a-zA-Z\-_]+)-([0-9]+)$</from>$3<to>/dopromotions/detail</to></规则>我的 Struts2 Action 有以下 getter 和 setter:
私有整数id;公共无效setId(整数ID){this.id = id;}公共整数getId(整数id){返回标识;}然而,变量永远不会被输入.调试时,我在参数或属性范围内的任何地方都找不到id.
我已经尝试删除 type="parameter".这将 id 放在属性范围内,但它不会输入到我的变量 Integer id 中.
解决方案我对你使用的 URL 重写器不熟悉,但你可以单独使用 Struts2 来实现这种映射.
请参考这个答案关于NamedVariablePatternMatcher.您需要在 struts.xml 中设置以下常量:
<constant name="struts.enable.SlashesInActionNames" value="true"/><constant name="struts.patternMatcher" value="namedVariable"/>然后,将您的操作映射为:
<!-- 您也可以将/promotions 设为命名空间,而操作只是abcdef-{id}"--><action name="promotions/abcdef-{id}" class="...">...</动作>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.
解决方案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>
更多推荐
在 Struts 2 中使用 UrlRewrite 设置参数变量
发布评论