Struts 2 中带有虚拟数据的通配符操作映射
我正在尝试使用通配符映射我的 Struts 操作。
之前,我使用了 Tuckey 的 UrlRewrite Filter。但是这个问题改变了我的想法。
所以这是我的问题:我的网址如下所示:
www.example.com/promoties/category-123
www.example.com/promoties/category-123/subcategory-456
在这些示例
中,单词 category
和 subcategory
是虚拟数据,用于使 URL 与搜索引擎更相关。
现在我想忽略这个虚拟数据,因为我只对(最后一个)ID 感兴趣。第一种情况为 123
,最后一种情况为 456
。
我尝试了以下方法,但没有成功:
<package name="promoties" namespace="/promoties" extends="struts-default">
<action name="([0-9a-zA-Z\-_]+)-{id:([0-9]+)}$" class="CategoryAction">
<result type="tiles">categorydetail</result>
</action>
</package>
在我的 struts.xml
中使用以下选项:
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.patternMatcher" value="regex" />
以前有人尝试过吗?我该如何在 Struts2 中做到这一点?
I'm trying to map my Struts actions using wildcards.
Before, I used UrlRewrite Filter by Tuckey. But this question changed my mind.
So here's my problem: My URL's look like the following:
www.example.com/promoties/category-123
www.example.com/promoties/category-123/subcategory-456
In these examples, the words category
and subcategory
are dummy data used to make the URL more relevant for search engines.
Now I'd like to ignore this dummy data, as I'm just interested in the (last) ID. In the first case 123
in the last case 456
.
I've tried the following without success:
<package name="promoties" namespace="/promoties" extends="struts-default">
<action name="([0-9a-zA-Z\-_]+)-{id:([0-9]+)}quot; class="CategoryAction">
<result type="tiles">categorydetail</result>
</action>
</package>
Using following options in my struts.xml
:
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.patternMatcher" value="regex" />
Has anyone tried this before? How would I go about doing this in Struts2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是使用简单的通配符映射,并将 id 组件的验证规范为 struts2 验证。这是一个已经过测试但未经验证的示例。
struts.xml 您将看到为 category-* 和 category-*/subcategory-* 定义的操作,我们只需保留第二张外卡。
test.TestBean 此处我使用了一个字符串,但在您的情况下,您将其更改为 int 或 Integer。您需要使用验证 xml 或简单地实现 com.opensymphony.xwork2.Validateable 来验证我们是否确实获得了一个整数。
/WEB-INF/content/test/results.jsp
示例 1
url:example.com/category-helloBart 产生...
通配符值
id:helloBart
示例 2
网址:example.com/category-helloBart/subcategory-123 产生...
通配符值
ID:123
One way is to use simple wild card mapping and regulate the validation of the id component to struts2 validation. Here is an example which has been tested, but without validation.
struts.xml you'll see an action defined for category-* and category-*/subcategory-* in the second we'll just keep the second wild card.
test.TestBean here I used a String but in your case you'll change this to int or Integer. You'll want to validate that we did get an integer using validation xml or simply implementing com.opensymphony.xwork2.Validateable.
/WEB-INF/content/test/results.jsp
Example 1
The url: example.com/category-helloBart produces...
Wild Card Value
id: helloBart
Example 2
The url: example.com/category-helloBart/subcategory-123 produces...
Wild Card Value
id: 123