While the prefix appears in the browser URI, the tags are "namespace aware", so the namespace prefix does not need to be embedded in forms and links.
struts.xml
:
<package name="testpkg" namespace="/test" extends="struts-default">
<action name="doTest" class="otes.test.TestAction">
<result>/success.jsp</result>
</action>
</package>
index.jsp
: (http://localhost:8080/nsdemo/
)
<h2>Using HTML tags:</h2>
<h3><a href="doTest">doTest without namespace</a></h3> <!-- 404 error -->
<h3><a href="test/doTest">doTest with namespace</a></h3> <!-- works -->
<h2>Using Struts2 tags:</h2>
<h3><s:a href="doTest">doTest without namespace (s:a href)</s:a></h3> <!-- 404 error -->
<h3><s:a href="test/doTest">doTest with namespace (s:a href)</s:a></h3> <!-- works -->
<!-- 404 error -->
<s:url action="doTest" var="myAction" />
<h3><s:a href="%{myAction}">doTest without namespace (s:url action)</s:a></h3>
<!-- works -->
<s:url action="test/doTest" var="myAction" />
<h3><s:a href="%{myAction}">doTest with namespace (s:url action)</s:a></h3>
这是否意味着我真的必须在表单和链接中指定 namespace ?
(如果重要的话,我正在使用 Struts 2.3.20。)
最佳答案
首先:文档中引用的标签是 Struts2 标签(例如 <s:url>
、 <s:a>
)。
并且命名空间感知意味着如果您已经在特定命名空间中执行了某些操作,那么在 JSP 中您不需要为 S2 链接和表单添加 当前 命名空间前缀.
例如如果你有这个包配置:
<package name="testpkg" namespace="/test" extends="struts-default">
<action name="index">/index.jsp</action>
<action name="doTest" class="otes.test.TestAction">
<result>/success.jsp</result>
</action>
</package>
并执行了index
行动(http://localhost/app/test/index.action)。然后在index.jsp中你可以编写
<s:a action="doTest">test</s:a>
该 URL 会将您带到相同的命名空间。
如果你想更改命名空间,有 namespace
您可以使用某些标签中的属性。
例如您在某个页面 ( http://localhost/app/index.action ) - 请注意 url 中没有命名空间,然后以下链接将执行 doTest
在/test
命名空间。
<s:a action="doTest" namespace="/test">test</s:a>
顺便说一句,不要向 S2 标记中的操作添加操作扩展。
这是错误:
<s:form action="doTest.action">
这是正确:
<s:form action="doTest">
关于java - 表单和链接中的 Struts2 命名空间需要前缀吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28052686/