Struts2 Package Configuration
The package element has one required attribute, name, which acts as the key for later reference to the package. The extends attribute is optional and allows one package to inherit the configuration of one or more previous packages - including all interceptor, interceptor-stack, and action configurations.
name 属性设定作用
1 在子package需要继承于这个父package。name 属性可以作为标示父package
2 便于多人协同开发(资料上没有写)
Note that the configuration file is processed sequentially down the document, so the package referenced by an "extends" should be defined above the package which extends it.
The optional abstract attribute creates a base package that can omit the action configuration.
Attribute | Required | Description |
name | yes | key to for other packages to reference |
extends | no | inherits package behavior of the package it extends |
namespace | no | |
Abstract | no | Declares package to be abstract (no action configurations required in package) |
为什么extends是可选属性但是在struts.xml中必须要配置extends="struts-default?
struts-defaul.xmt是struts-core包中的默认配置文件。是无法修改的:其中配置name属性值为” struts-defaul”package标签如下:
<package name="struts-default" abstract="true">
省略其内容
</package>
struts.xml 基于此的,所以必须要配置extends="struts-default。而在
namespace
namespace attribute subdivides action configurations into logical modules, each with its own identifying prefix. Namespaces avoid conflicts between action names. Each namespace can have its own "menu" or "help" action, each with its own implementation. 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 2 Namespaces are the equivalent of Struts Action 1 modules, but more convenient and flexible. |
The default namespace is "" - an empty string. The default namespace is used as a "catch-all" namespace. If an action configuration is not found in a specified namespace, the default namespace is also be searched. The local/global strategy allows an application to have global action configurations outside of the action element "extends" hierarchy.
The namespace prefix can be registered with Java declarative security, to ensure only authorized users can access the actions in a given namespace.
Default Namespace 相当于其他所有Namespace的根目录,(目录=请求url)
以tomcat服务器为例 假设项目名称是struts2
那么Default Namespace 是http://localhost:8080/struts2_2
如果 jsp页面 A 的form 设定如下
<form action="<%=path%>/xuxl1/xuxl2/namespace.action" method="post">
省略内容
</form>
如果 jsp页面B的form 设定如下
<form action="<%=path%>/xuxl1/namespace.action" method="post">
省略内容
</form>
其中String path = request.getContextPath()
Struts.xml 配置如下
<package name="struts" extends="struts-default">
<action name="namespace" class="xuxl.struts.action.DefaultNameSpacetTest">
<result name="success">/DefaultNamespaceSuccess.jsp</result>
</action>
</package>
<package name="xuxl" extends="struts-default" namespace="/xuxl1/xuxl2">
<action name="namespace" class="xuxl.struts.action.NameSpacetTest">
<result name="success">/NamespaceSuccess.jsp</result>
</action>
</package>
页面A 提交请求路径为http://localhost:8080/struts2_2 /xuxl1/xuxl2/namespace.action
那么 这个请求就由xuxl.struts.action.NameSpacetTest 处理
页面B 提交请求路径为http://localhost:8080/struts2_2 /xuxl1/namespace.action
在struts.xml文件中没有定义namespace=” /xuxl1” 的package 标签
在Default Namespace 中配置了<action name="namespace" class="xuxl.struts.action.DefaultNameSpacetTest">
所以页面B 提交请求xuxl.struts.action.DefaultNameSpacetTest 处理
Namespace prefix 权限访问服务器资源
A root namespace ("/") is also supported. The root is the namespace when a request directly under the context path is received. As with other namespaces, it will fall back to the default ("") namespace if a local action is not found.
<package name="xuxl2" extends="struts-default" namespace="/">
<action name="root" class="xuxl.struts.action.RootTest">
<result name="success">/Root.jsp</result>
</action>
输入请求url : http://localhost:8080/struts2_2/root.action
便可以调用action:xuxl.struts.action.RootTest
Namespace Example
<package name="default">
<action name="foo" class="mypackage.simpleAction">
<result name="success" type="dispatcher">greeting.jsp</result>
</action>
<action name="bar" class="mypackage.simpleAction">
<result name="success" type="dispatcher">bar1.jsp</result>
</action>
</package>
<package name="mypackage1" namespace="/">
<action name="moo" class="mypackage.simpleAction">
<result name="success" type="dispatcher">moo.jsp</result>
</action>
</package>
<package name="mypackage2" namespace="/barspace">
<action name="bar" class="mypackage.simpleAction">
<result name="success" type="dispatcher">bar2.jsp</result>
</action>
</package>
If a request for /barspace/bar.action is made, the /barspace namespace is searched for the bar action. If found, the bar action is executed, else it will fall back to the default namespace. In the Namespace Example, the bar action does exist in the /barspace namespace, so the bar action will be executed, and if "success" is returned, the request will be forwarded to bar2.jsp.
| Falling Back to Foo If a request is made to /barspace/foo.action, the namespace /barspace will be checked for action foo. If a local action is not found, the default namespace is checked. In the Namespace Example, there is no action foo in the namespace /barspace, therefore the default will be checked and /foo.action will be executed. |
In the Namespace Example, if a request for moo.action is made, the root namespace ('/') is searched for a moo action; if a root action is not found, the default namespace is checked. In this case, the moo action does exist and will be executed. Upon success, the request would be forwarded to bar2.jsp.
| Getting to the Root If a request is made for /foo.action, the root / namespace will be checked. If foo is found, the root action will be selected. Otherwise, the framework will check the default namespace. In the Namespace Example, the foo action does not exist in the root namespace, so the default namespace is checked, and the default foo action is executed. |
| Namespaces are not a path! Namespace are not hierarchical like a file system path. There is one namespace level. For example if the URL /barspace/myspace/bar.action is requested, the framework will first look for namespace /barspace/myspace. If the action does not exist at /barspace/myspace, the search will immediately fall back to the default namespace "". The framework will not parse the namespace into a series of "folders". In the Namespace Example, the bar action in the default namespace would be selected. |
1 按照请求的路径,匹配package ,若匹配成功再匹配action
2 匹配失败直接,到default namespace 匹配
最多匹配 两次匹配package