Struts2 Package

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需要继承于这个父packagename 属性可以作为标示父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

see Namespace Configuration

Abstract

no

Declares package to be abstract (no action configurations required in package)

 

为什么extends是可选属性但是在struts.xml中必须要配置extends="struts-default

struts-defaul.xmtstruts-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.

Default Namespace

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页面Bform 设定如下

<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 权限访问服务器资源

Root Namespace

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>

         </package>

输入请求url : http://localhost:8080/struts2_2/root.action 

便可以调用actionxuxl.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>

How the Code Works

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 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值