Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
781 views
in Technique[技术] by (71.8m points)

url rewriting - Wildcard action mapping with dummy data in Struts 2

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]+)}$" 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...