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

-Bean

Strus2在内部使用一个独自的依赖注入容器,初始化时载入了框架的关键对象,使整个框架的任意模块可以被替换、继承、删除通过一个统一的标准方式。特别对于插件,可以利用这个方式来拓展框架,提供对第三方类库的支持。


Attribute

Required

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 defaultsingletonrequestsessionthread

(该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)
<struts>
 
   <bean type="com.opensymphony.xwork2.ObjectFactory" name="myfactory" class ="com.company.myapp.MyObjectFactory" />
   
   ...
 
</struts>




-Constant 


Constant提供一个简单的方式用于修改Struts2框架和其插件的参数,其中有两个重要的功能:

1.修改一些参数,如最大上传文件的大小,使用使用“DevMode”等等。

                2.当多个bean实现了同一接口,应该进行选择使用哪一个。


Constant可以被定义在多个文件中,将按照如下的顺序进行搜索,后面的文件中的Constant会覆盖之前的。

1.struts-default.xml

  2.struts-plugin.xml

  3.struts.xml

4.struts.properties

5.web.xml


在XML中的定义

 

 Attribute

Required

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)
< struts >
 
   < constant name="struts.devMode" value="true" />
 
   ...
 
</ struts >
Constant Example (struts.properties)
struts.devMode = true
Constant Example (web.xml)
< web-app id="WebApp_9" version="2.4"
 
     < filter >
         < filter-name >struts</ filter-name >
         < filter-class >org.apache.struts2.dispatcher.FilterDispatcher</ filter-class >
         < init-param >
             < param-name >struts.devMode</ param-name >
             < param-value >true</ param-value >
         </ init-param >
     </ filter >
 
     ...
 
</ web-app >


-Package

package将actions, results, result types, interceptors,和 interceptor-stacks 组成一个配置单元,并且类似于一个对象可以被继承和重载。

Attribute

Required

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)
<struts>
   < package name="employee" extends ="struts- default " namespace="/employee">
     < default -interceptor-ref name="crudStack"/>
 
     <action name="list" method="list"
       class ="org.apache.struts2.showcase.action.EmployeeAction" >
         <result>/empmanager/listEmployees.jsp</result>
         <interceptor-ref name="basicStack"/>
     </action>
     <action name="edit-*" class ="org.apache.struts2.showcase.action.EmployeeAction">
       <param name="empId">{ 1 }</param>
       <result>/empmanager/editEmployee.jsp</result>
         <interceptor-ref name="crudStack">
           <param name="validation.excludeMethods">execute</param>
         </interceptor-ref>
       </action>
       <action name="save" method="save"
           class ="org.apache.struts2.showcase.action.EmployeeAction" >
         <result name="input">/empmanager/editEmployee.jsp</result>
         <result type="redirect">edit-${currentEmployee.empId}.action</result>
       </action>
       <action name="delete" method="delete"
         class ="org.apache.struts2.showcase.action.EmployeeAction" >
         <result name="error">/empmanager/editEmployee.jsp</result>
         <result type="redirect">edit-${currentEmployee.empId}.action</result>
       </action>
   </ package >
</struts>

Inherit from more than one package

Multi package Example (struts.xml)
<struts>
   < package name="employee" extends ="struts- default , json- default " namespace="/employee">
 
     <action name="list" method="list" class ="org.apache.struts2.showcase.action.EmployeeAction" >
         <result>/empmanager/listEmployees.jsp</result>
         <result type="json">
             <param name="root">employees</param>
         </result>
     </action>
 
   </ package >
</struts>


-Namespace


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

 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 >


当请求     /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中



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值