Struts 2 中带有虚拟数据的通配符动作映射 [英] Wildcard action mapping with dummy data in Struts 2
问题描述
我正在尝试使用通配符映射我的 Struts 操作.
I'm trying to map my Struts actions using wildcards.
之前,我使用了 Tuckey 的 UrlRewrite Filter.但是这个问题改变了我的想法.
Before, I used UrlRewrite Filter by Tuckey. But this question changed my mind.
所以这是我的问题:我的 URL 如下所示:
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
在这些示例中,词 category
和 subcategory
是用于使 URL 与搜索引擎更相关的虚拟数据.
In these examples, the words category
and subcategory
are dummy data used to make the URL more relevant for search engines.
现在我想忽略这个虚拟数据,因为我只对(最后一个)ID 感兴趣.在第一种情况下 123
在最后一种情况下 456
.
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
.
我尝试了以下方法但没有成功:
<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
中使用以下选项:
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" />
以前有人试过吗?我将如何在 Struts2 中执行此操作?
Has anyone tried this before? How would I go about doing this in Struts2?
推荐答案
一种方法是使用简单的通配符映射,规范id组件的验证到struts2的验证.这是一个经过测试但未经验证的示例.
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 您将在第二次看到为 category-* 和 category-*/subcategory-* 定义的操作'将只保留第二张通配符.
struts.xml you'll see an action defined for category-* and category-*/subcategory-* in the second we'll just keep the second wild card.
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<package namespace="" name="default" extends="struts-default">
<action name="category-*" class="test.TestBean">
<param name="id">{1}</param>
<result>/WEB-INF/content/test/results.jsp</result>
</action>
<action name="category-*/subcategory-*" class="test.TestBean">
<param name="id">{2}</param>
<result>/WEB-INF/content/test/results.jsp</result>
</action>
</package>
</struts>
test.TestBean 在这里我使用了字符串,但在您的情况下,您将其更改为 int 或 Integer.您需要使用验证 xml 或简单地实现 com.opensymphony.xwork2.Validateable 来验证我们确实获得了一个整数.
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.
package test;
import com.opensymphony.xwork2.ActionSupport;
public class TestBean extends ActionSupport{
//public to avoid get/set to make example shorter
public String id;
}
/WEB-INF/content/test/results.jsp
/WEB-INF/content/test/results.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<h1>Wild Card Value</h1>
id: <s:property value="id"/>
</body>
</html>
示例 1网址:example.com/category-helloBart 产生...
Example 1 The url: example.com/category-helloBart produces...
通配符价值
id: helloBart
id: helloBart
示例 2网址:example.com/category-helloBart/subcategory-123 产生...
Example 2 The url: example.com/category-helloBart/subcategory-123 produces...
通配符价值
id:123
这篇关于Struts 2 中带有虚拟数据的通配符动作映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!