看下面的一张图来了解一个url匹配struts 2的动作命名空间。
p.s 包中的“name”不会影响结果,只是给一个有意义的名字。
struts.xml
<?xml version="1.0" encoding="utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="saywelcome"> <result>/pages/welcome.jsp</result> </action> </package> <package name="common" namespace="/common" extends="struts-default"> <action name="saywelcome"> <result>/common/pages/welcome.jsp</result> </action> </package> <package name="user" namespace="/user" extends="struts-default"> <action name="saywelcome"> <result>/common/user/welcome.jsp</result> </action> </package> </struts>
struts 2的动作命名空间映射到文件夹结构。
3 jsp页面视图具有相同的文件名,但是在不同的模块位置。
根 – pages/welcome.jsp
<html> <head> <title>struts2命名空间示例 - h3.com</title> </head> <body> <h1>struts2命名空间示例</h1> <h4>welcome - namespace = "root"</h4> </body> </html>
common 模块 – common/pages/welcome.jsp
<html> <head> <title>struts2命名空间示例 - h3.com</title> </head> <body> <h1>struts2命名空间示例</h1> <h4>welcome - namespace = "common"</h4> </body> </html>
user 模块 – user/pages/welcome.jsp
<html> <head> <title>struts2命名空间示例 - h3.com</title> </head> <body> <h1>struts2命名空间示例</h1> <h4>welcome - namespace = "user"</h4> </body> </html>
示例 1
url : http://localhost:8080/struts2example/saywelcome.action
将匹配根命名空间。
<package name="default" namespace="/" extends="struts-default"> <action name="saywelcome"> <result>/pages/welcome.jsp</result> </action> </package>
这会显示 pages/welcome.jsp 页面的内容
示例 2
url : http://localhost:8080/struts2example/common/saywelcome.action
这会匹配 common 命名空间的内容:
<package name="common" namespace="/common" extends="struts-default"> <action name="saywelcome"> <result>/common/pages/welcome.jsp</result> </action> </package>
这会显示 common/pages/welcome.jsp 页面的内容
示例 3
url : http://localhost:8080/struts2example/user/saywelcome.action
这会匹配 common 命名空间的内容:
<package name="user" namespace="/user" extends="struts-default"> <action name="saywelcome"> <result>/common/user/welcome.jsp</result> </action> </package>
这会显示 user/pages/welcome.jsp 页面的内容.
代码下载:http://pan.baidu.com/s/1hqe1nze