Struts2 通配符映射 - 更具体的映射由通用映射处理
Struts2 Wildcard Mapping - more specific one is being handled by generic one
我目前正在研究我的 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表示最后一个获胜
编辑:
刚刚尝试将它们切换过来,结果成功了!我是不是误读了文档?
您在操作名称中使用了斜杠,这不正确地与通配符映射器一起使用。正如我在链接 answer 中所说,在这种情况下最好的模式匹配器是 regex
模式匹配器。
<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 application:
<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 将按以下顺序匹配:
http://localhost:8080/example/HelloWorld
http://localhost:8080/example/Login_input
http://localhost:8080/example/Register
我会说更具体的映射先于更少的 specific/common 映射并且它获胜,因为它在操作配置的顺序中首先找到。与有序配置不匹配的所有内容都属于不太具体的最后一个映射。
在你的情况下,来自官方文档,
Mappings are matched against the request in the order they appear in the framework’s configuration file. If more than one pattern matches the last one wins, so less specific patterns must appear before more specific ones. However, if the request URL can be matched against a path without any wildcards in it, no wildcard matching is performed and order is not important.
https://struts.apache.org/core-developers/wildcard-mappings.html#wildcards
我目前正在研究我的 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表示最后一个获胜
编辑:
刚刚尝试将它们切换过来,结果成功了!我是不是误读了文档?
您在操作名称中使用了斜杠,这不正确地与通配符映射器一起使用。正如我在链接 answer 中所说,在这种情况下最好的模式匹配器是 regex
模式匹配器。
<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 application:
<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 将按以下顺序匹配:
http://localhost:8080/example/HelloWorld
http://localhost:8080/example/Login_input
http://localhost:8080/example/Register
我会说更具体的映射先于更少的 specific/common 映射并且它获胜,因为它在操作配置的顺序中首先找到。与有序配置不匹配的所有内容都属于不太具体的最后一个映射。
在你的情况下,来自官方文档,
Mappings are matched against the request in the order they appear in the framework’s configuration file. If more than one pattern matches the last one wins, so less specific patterns must appear before more specific ones. However, if the request URL can be matched against a path without any wildcards in it, no wildcard matching is performed and order is not important.
https://struts.apache.org/core-developers/wildcard-mappings.html#wildcards