struts.xml中package元素的namespace属性中的“/”是什么意思?

时间:2014-01-28 18:42:40

标签: java struts

我是struts中的新手,所以我知道package命名空间中“/”的含义。 即  当我把命名空间作为“/ Home”

 <package name="base" namespace="/Home" extends="struts-default">
            <action name="HelloWorld" class="controller.HelloWorld">
                <result>/message.jsp</result>
            </action>
    </package>

在浏览器中键入此Url:“localhost:8080 / Struts2Example / Home / HelloWorld.action”,然后打开页面。

但是当我将名称空间更改为“Home”时,它会显示“Error:404”。

那么“/”是什么意思?

3 个答案:

答案 0 :(得分:1)

来自Struts手册http://download.oracle.com/otn_hosted_doc/jdeveloper/j2ee101302/working_with_struts/f1_struts_ide/f1_strutsconfigactionmappings.html

在这里

http://struts.apache.org/release/2.1.x/docs/namespace-configuration.html

请求URI路径,当由Struts servlet操作控制器接收时,调用操作映射。 The path name is a relative path and must include the forward slash (/).

答案 1 :(得分:1)

这是名字空间上的101个。  您需要记住的第一件事是命名空间不是文件路径。

来到命名空间的作用: 当用户点击访问struts Web应用程序的请求时,会发生这种情况,该URL被分为主机名,端口名,servlet上下文名,命名空间(如果有)和struts操作。

实施例。 :localhost:8080 / myapp / discountModule / getPrice.action

命名空间将操作配置划分为逻辑模块。 例如,如果请求URL /foo/myfoo/myfooaction.action,框架将首先查找namespace / foo / myfoo。如果/ foo / myfoo中不存在该操作,则搜索将立即回退到默认命名空间“”。框架不会将命名空间解析为一系列“文件夹”。

所以我们何时使用名称Spaces 命名空间在需要分成小域或模块的大型Web应用程序中非常有用。示例假设您正在管理购物车后端,这有4个主要部分 - ADMIN,ORDERS,SALES&amp; REFUNDS

如前所述,使用namespace属性在标记内定义名称空间。要定义这四个域,将使用其命名空间规范定义四个标记,如下所示:

Ex - localhost:8080 / shoppingCart / admin / doSomeAdmin.action

记住 - 使用一个NAMESPACE并不是手段。

Struts文档中还有更多示例 - http://struts.apache.org/release/2.1.x/docs/namespace-configuration.html

答案 2 :(得分:0)

添加到user1428716 只有“/”定义根名称空间。

<package name="base" namespace="/e" extends="struts-default">
            <action name="HelloWorld" class="controller.HelloWorld">
                <result>/message.jsp</result>
            </action>
    </package>

root是:“http://localhost:8080/Struts2Example/” 所以网址是“http://localhost:8080/Struts2Example/HelloWorld.action

当我们提到"/namespacename"时,则表示"~root/namespaceName/your.action"