表单和链接中的Struts2名称空间是否需要前缀? [英] Is prefix needed for Struts2 namespace in forms and links?

查看:91
本文介绍了表单和链接中的Struts2名称空间是否需要前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apache文档:

当前缀出现在浏览器URI中时,标记为命名空间 ",因此无需将名称空间前缀嵌入表格中 和链接.

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:

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>

这是否意味着我真的必须在表单和链接中指定名称空间?

Does this mean I really have to specify the namespace in my forms and links?

(如果重要的话,我正在使用Struts 2.3.20.)

(I am using Struts 2.3.20 if that matters.)

推荐答案

首先:文档中引用的标签是Struts2标签(例如<s:url><s:a>).

First of all: the tags referred in the docs are Struts2 tags (e.g. <s:url>, <s:a>).

并且名称空间感知意味着,如果您已经在特定的名称空间中执行了某些操作,则在JSP中,您不需要为S2链接和表单添加 current 前缀.

And namespace aware means that if you already executed some action in particular namespace then in the JSP you don't need to prefix S2 links and forms with the current namespace.

例如如果您具有以下程序包配置:

E.g. if you have this package configuration:

<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中可以编写

and executed the index action (http://localhost/app/test/index.action). Then in index.jsp you can write

<s:a action="doTest">test</s:a>

,该网址会将您带到相同的名称空间.

and that url will take you to the same namespace.

如果要更改名称空间,则可以使用某些标记中的namespace属性.

If you want to change namespace there is namespace attribute in some tags that you can use.

例如您在某个页面中( http://localhost/app/index.action )-请注意,其中没有命名空间网址,则以下链接将在/test命名空间中执行doTest.

E.g. you are in some page (http://localhost/app/index.action) - note no namespace in url, then following link will execute doTest in /test namespace.

<s:a action="doTest" namespace="/test">test</s:a>

顺便说一句,不要在S2标签中的操作中添加操作扩展.

BTW don't add action extension to actions in S2 tags.

这是错误:

<s:form action="doTest.action"> 

这是正确:

<s:form action="doTest">

这篇关于表单和链接中的Struts2名称空间是否需要前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆