您的位置:首页 > 编程语言 > Java开发

[Struts2官方指南的个人学习和翻译] Struts2的配置元素-管理元素

2015-04-18 13:31 405 查看
-BeanStrus2在内部使用一个独自的依赖注入容器,初始化时载入了框架的关键对象,使整个框架的任意模块可以被替换、继承、删除通过一个统一的标准方式。特别对于插件,可以利用这个方式来拓展框架,提供对第三方类库的支持。
AttributeRequired
Description
class
yes
the name of the bean class (对应的Java类的名字)
type
no
the primary Java interface this class implements (该类实现了的主要接口)
name
no
the unique name of this bean; must be unique among other beans that specify the same
type
(该Bean的名字可以用来区别于其他Bean)

scope
no
the scope of the bean; must be either
default
,
singleton
,
request
,
session
,
thread
(该bean的作用范围)

static
no
whether to inject static methods or not; shouldn't be
true
when the
type
is specified(是否注入静态方法,当type被指定时不能选择true)
optional
no
whether the bean is optional or not (该bean是否为可选的)
Bean元素必须要有class属性来指定其对应的Java类,才能进行对对象的创建和操作。


Sample usage

Bean Example (struts.xml)
-ConstantConstant提供一个简单的方式用于修改Struts2框架和其插件的参数,其中有两个重要的功能:1.修改一些参数,如最大上传文件的大小,使用使用“DevMode”等等。2.当多个bean实现了同一接口,应该进行选择使用哪一个。Constant可以被定义在多个文件中,将按照如下的顺序进行搜索,后面的文件中的Constant会覆盖之前的。1.struts-default.xml2.struts-plugin.xml3.struts.xml4.struts.properties5.web.xml在XML中的定义
AttributeRequired
Description
name
yes
the name of the constant
value
yes
the value of the constant
如在web.xml中,FilterDispatcher的参数都用constant来定义。


Sample usage

Constant Example (struts.xml)
Constant Example (struts.properties)
Constant Example (web.xml)
-Packagepackage将actions, results, result types, interceptors,和 interceptor-stacks 组成一个配置单元,并且类似于一个对象可以被继承和重载。
AttributeRequired
Description
name
yes
key to for other packages to reference (用于对一个package的唯一标识)
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)


Simple usage

Package Example (struts.xml)


Inherit from more than one package

Multi package Example (struts.xml)


-Namespace

用于对action的划分,解决action名字的冲突问题,默认存在于default中。


Namespace Example

当请求
/barspace/bar.action ,先在barspace中寻找bar,若找不到则回退到default寻找,在上例中,barspace中存在bar,将跳转至
bar2.jsp。
当请求 /barspace/foo.action
, 在上例中,barspace中不存在foo,则回退到default中寻找,所以将跳转至greeting.jsp。当请求/
moo.action,会先在/空间中寻找,上例中/中存在moo,则将会跳转至moo.jsp
当请求 /foo.action,先在/空间中寻找,上例中/中不存在foo,回退至default中寻找,最后结果为greeting.jsp* namaspace和路径不同,没有层次划分,例如请求
/barspace/myspace.bar.action,在上例不存在该namespace,将会回退至default中寻找而不是barspace中。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: