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

时间:2016-04-27 06:57:25

标签: java struts2 pattern-matching mapping wildcard

我目前正在使用我的Struts2配置进行通配符测试,我一直坚持使用此功能。

    <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

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

我在这里做错了吗? struts wildcard doc表示最后一个将获胜

编辑: 只是试图切换它们,它工作O_O。我误读了医生吗?

1 个答案:

答案 0 :(得分:3)

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

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

请参阅Advanced Wildcards

<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将按顺序匹配:

  1. http://localhost:8080/example/HelloWorld
  2. http://localhost:8080/example/Login_input
  3. http://localhost:8080/example/Register
  4. 我想说更具体的映射会在不太具体/常见的映射之前进行,并且它会获胜,因为它首先按动作配置的顺序找到。与有序配置不匹配的所有内容都属于最后一个不太具体的映射。

相关问题