Struts2 通配符映射 - 更具体的映射由通用映射处理

我目前正在使用我的 Struts2 配置进行通配符测试,但我坚持使用这个配置.

I'm currently playing around with my Struts2 config for wildcard testing and I'm stuck with this one.

    <action name="/*/*" class="checkBlogUrl" method="testing">
        <param name="blogSiteUrl">{1}</param>
        <param name="test">{2}</param>
        <result name="success">/WEB-INF/jsp/cmsPages/index.jsp</result>
    </action>
    
    <action name="/*/postPreview1" class="blogPostAction" method="test">
        <param name="blogSiteUrl">{1}</param>
        <result name="success">/WEB-INF/jsp/cmsPages/templatePicker.jsp</result>
    </action>

如果我访问 myurl.com/hello/hi,我将被重定向到 index.jsp.

If I access myurl.com/hello/hi I will be redirected to index.jsp.

但如果我访问 myurl.com/hello/postPreview1,我也会被重定向到 index.jsp 而不是 templatePicker.jsp.

But if I access myurl.com/hello/postPreview1 I will also be redirected to index.jsp instead of templatePicker.jsp.

我在这里做错了吗?struts wildcard doc说最后一个会赢

Am I doing something wrong here? The struts wildcard doc said that the last one will win

刚刚尝试切换它们并且成功了!我是否误读了文档?

Just tried to switch them around and it worked! Am I misreading the doc?

推荐答案

您在动作名称中使用斜杠,这与通配符映射器一起使用不正确.正如我在链接的 answer 中所说,在这种情况下最好的模式匹配器是 regex 模式匹配器.

You are using slashes in action name, that incorrectly works with wildcard mapper. As I said in the linked answer, the best pattern matcher in this case is the regex pattern matcher.

<constant name="struts.patternMatcher" value="regex"/>

请参阅高级通配符.

<action name="/{blogSiteUrl}/{test}" class="checkBlogUrl" method="testing">
    <result name="success">/WEB-INF/jsp/cmsPages/index.jsp</result>
</action>

<action name="/{blogSiteUrl}/postPreview1" class="blogPostAction" method="test">
    <result name="success">/WEB-INF/jsp/cmsPages/templatePicker.jsp</result>
</action>


关于通配符映射器的文档.让我们看一下示例 blank 应用程序:

<package name="example" namespace="/example" extends="default">

    <action name="HelloWorld" class="example.HelloWorld">
        <result>/WEB-INF/jsp/example/HelloWorld.jsp</result>
    </action>

    <action name="Login_*" method="{1}" class="example.Login">
        <result name="input">/WEB-INF/jsp/example/Login.jsp</result>
        <result type="redirectAction">Menu</result>
    </action>

    <action name="*" class="example.ExampleSupport">
        <result>/WEB-INF/jsp/example/{1}.jsp</result>
    </action>

    <!-- Add actions here -->
</package>


所以 URL 将按顺序匹配:


So URLs will be matched in the order:

  1. http://localhost:8080/example/HelloWorld
  2. http://localhost:8080/example/Login_input
  3. http://localhost:8080/example/Register

我会说更具体的映射先于不太具体/常见的映射,它会获胜,因为它在操作配置的顺序中首先找到.与有序配置不匹配的所有内容都属于最后一个不太具体的映射.

I would say that more specific mapping goes before less specific/common mapping and it wins because it's found first in the order of action configs. Everything that doesn't match the ordered configs fall into last mapping which is less specific.

相关文章