【张冰Struts2学习札记】0402.struts.xml配置详解之二 名称空间 包含
struts.xml 配置详解之二 名称空间 包含
1. 名称空间
package 元素的namespace 属性可以将包中的action 配置为不同的名称空间 ,这样就可以在不同的名称空间 中使用同名action 。Struts2 框架使用名称空间和action 的名字来标识一个action 。
1.1 、自定义名称空间与默认名称空间
默认的名称空间 是空字符串””,也就是不设置namespace 属性时候的名称空间 。我们在匹配一个action 的时候,先 到它指定的名称空间 中去找,如果没有 再到这个默认的名称空间 中去找。
代码清单 1 : struts.xml 片段
< package name = "loginTest" extends = "struts-default">
< action name = "login" class = "com.coderdream.action.LoginAction">
< result name = "success"> /loginSuc.jsp
</ result >
< result name = "input"> /loginFail.jsp
</ result >
</ action >
< action name = "test1 " class = "com.coderdream.action.Test1Action">
< result name = "success"> /test1-1.jsp
</ result >
</ action >
</ package >
< package name = "test1" extends = "struts-default" namespace = "/test1 ">
< action name = "test1 " class = "com.coderdream.action.Test1Action">
< result name = "success"> /test1.jsp
</ result >
</ action >
</ package >
代码清单 2 : mail.jsp 片段
<a href = " <%= path %> /test1.action" >test1 </a ><br />
访问: test1-1.jsp 文件(未指定名称空间,相当于指定为根名称空间,未定义根名称空间,会直接去默认的名称空间找)
<a href = " <%= path %> /test1/test1.action" >test1 </a ><br />
访问: test1.jsp 文件(指定名称空间,直接去指定名称空间找)
<a href = " <%= path %> /test100/test1.action" >test1 </a ><br />
<a href = " <%= path %> /test200/test1.action" >test1 </a ><br />
<a href = " <%= path %> /test500/test1.action" >test1 </a ><br />
访问: test1-1.jsp 文件(指定名称空间,直接去指定名称空间找,如果找不到,会去默认的名称空间找)
1.2 、根名称空间
Struts2 还支持根名称空间( “/ ”) ,当一个request 直接请求context path 下面的资源时,struts2 会首先到跟名称空间下去寻找匹配的action ,例如请求是:
http://ip/websitename/xxx.action
那么我们首先会去”/ ”名称空间 下去寻找这个action ,然后再去默认的名称空间 找。
代码清单 3 : struts.xml 片段
< package name = "loginTest" extends = "struts-default">
< action name = "test2" class = "com.coderdream.action.Test1Action">
< result name = "success"> /msg /test1-1.jsp
</ result >
</ action >
</ package >
< package name = "test1" extends = "struts-default" namespace = "/test1">
< action name = "test1" class = "com.coderdream.action.Test1Action">
< result name = "success"> /msg/test1.jsp
</ result >
</ action >
</ package >
< package name = "test11" extends = "struts-default" namespace = "/">
< action name = "test1" class = "com.coderdream.action.Test1Action">
< result name = "success"> /msg/test1-2.jsp
</ result >
</ action >
</ package >
代码清单 4 : mail.jsp 片段
<a href = " <%= path %> /test1.action" >test1 </a ><br />
访问: test1-2.jsp 文件(指定为根名称空间,去定义根名称空间找)
注意:
名称空间和文件系统的路径具有多级目录不同,名称空间只有一个级别 。例如,如果请求URL :/barspace/myspace/bar.action ,框架将首先在“/barspace/myspace ” 名称空间中查找,如果没有 找到bar.action ,框架将直接到默认的名称空间中查找 。框架并不会将名称空间解析为一系列的”文件夹“。
提示:
多个包 可以映射到相同的名称空间 ,这与包所要求的唯一性是不同的。
2. 包含( include )配置
在大型项目开发中,可以将项目分解为多个小模块,每个模块独立开发和管理。我们可以为每个模块提供一个配置文件,然后对其进行配置,然后在struts.xml 中使用include 元素包含这些配置文件。
例如:
代码清单 5 : struts.xml 片段
<include file="struts-chat.xml" />
另外,可以引用特定目录下的文件,如存在struts.xml 放在com/coderdream/vo 下,则包含配置如下:
代码清单 6 : struts.xml 片段
<include file="com/coderdream/vo/struts-user.xml" />
我的联系方式: 85337464
我的博客: http://coderdream.iteye.com