我目前正在使用我的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。我误读了医生吗?
答案 0 :(得分:3)
您正在使用操作名称中的斜杠,这与使用通配符映射器不正确。正如我在链接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 应用程序:
<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
我想说更具体的映射会在不太具体/常见的映射之前进行,并且它会获胜,因为它首先按动作配置的顺序找到。与有序配置不匹配的所有内容都属于最后一个不太具体的映射。