-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 |
scope | no | the scope of the bean; must be either |
static | no | whether to inject static methods or not; shouldn't be (是否注入静态方法,当type被指定时不能选择true) |
optional | no | whether the bean is optional or not (该bean是否为可选的) |
Bean元素必须要有class属性来指定其对应的Java类,才能进行对对象的创建和操作。
Sample usage
<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
<
struts
>
<
constant
name="struts.devMode" value="true" />
...
</
struts
>
|
struts.devMode =
true
|
<
web-app
id="WebApp_9" version="2.4"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<
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
<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
<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
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
/barspace/myspace.bar.action,在上例不存在该namespace,将会回退至default中寻找而不是barspace中。