java - 表单和链接中的 Struts2 命名空间需要前缀吗?

标签 java struts2 namespaces

来自Apache documentation :

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/

相关文章:

java - Eclipse 类路径条目仅用于测试

java - Google 应用引擎上的 Struts 2。问题

c++ - 实现在匿名命名空间中声明的函数时出现 undefined reference 链接器错误

java - 哪种面向对象的 GUI 设计能够正确地结合命令模式和观察者模式?

java - WheelView 不会在 ScrollView 内滚动

从 Web 服务器下载文件时出现 java.lang.IllegalStateException servlet 异常

java - 如何在不执行操作的情况下从 struts.xml 本身返回 HTML 页面?

php - 什么时候可以在 Symfony2 中创建两个同名的 Controller ?

PHP不同的路径名空间和自动加载不起作用

java - 当我在同一执行中多次使用 HtmlUnit 请求时,收到 502 Bad Gateway