Struts 2 中带有虚拟数据的通配符操作映射

发布于 2024-10-28 04:26:08 字数 1220 浏览 1 评论 0原文

我正在尝试使用通配符映射我的 Struts 操作。

之前,我使用了 Tuckey 的 UrlRewrite Filter。但是这个问题改变了我的想法。

所以这是我的问题:我的网址如下所示:

  • www.example.com/promoties/category-123
  • www.example.com/promoties/category-123/subcategory-456 在这些示例

中,单词 categorysubcategory 是虚拟数据,用于使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-11-04 04:26:08

一种方法是使用简单的通配符映射,并将 id 组件的验证规范为 struts2 验证。这是一个已经过测试但未经验证的示例。

struts.xml 您将看到为 category-*category-*/subcategory-* 定义的操作,我们只需保留第二张外卡。

<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 来验证我们是否确实获得了一个整数。

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

<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <body>
        <h1>Wild Card Value</h1>
        id: <s:property value="id"/>
    </body>
</html>

示例 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.

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

<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <body>
        <h1>Wild Card Value</h1>
        id: <s:property value="id"/>
    </body>
</html>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文