在 Struts 2 中使用 UrlRewrite 设置参数变量

编程入门 行业动态 更新时间:2024-10-24 08:27:14
本文介绍了在 Struts 2 中使用 UrlRewrite 设置参数变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我将 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 设置参数变量

本文发布于:2023-11-01 02:25:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547985.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   参数   Struts   UrlRewrite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!