20090319 (XWork)

本文介绍了Struts2框架的基本配置方法,包括Action标签的使用方式、默认执行流程及参数设置,Result标签的默认属性及其配置细节,并探讨了拦截器、包配置、ValueStack等核心组件的工作原理。

Action
The <action> tag can use any type of object, by default, it will call the execute method, you can override this by specifying a value for the "method" attribute, also, use "actionName!methodName" to force to invoke a public method in action.
If the "class" attribute is not specified, "ActionSupport" will be used.
You can define a default action inside a package which will be executed when no match is found.


ActionProxy,ActionInvocation,ActionContext


Result
<result name="success" type="dispatcher">
    <param name="location">/thank_you.jsp</param>
</result>
The default value for "name" attribute is "success", the default value for "type" is "dispatcher", and if there's no <param> tag to specify "location", the text inside <result> tag will be set to "location" attribute.


Interceptor,InterceptorStack,PreResultListener(should be add to ActionInvocation programatically)


Package, what's the usage of abstract and extend? When marked with "abstract", a package's action definition will not be inherit by it's child package?
Package is used to group actions into namespace (think of it as context path in webapp), so that different actions are invoked by different context path prefix ("/some.do", "/myapp/some.do") and share the same action alias.
A default namespace is empty string "", which will be used as fallback namespace, when not able to find action in a certain package, package with default namespace will be searched.
A root namespace is "/", which means that context path is empty.
In contrast to package namespace, package name is used for action reference.


Use <include file="xwork-default.xml"/> to include configuration file.


ValueStack, evaluate expression from head to tail, both ActionInvocation, ActionContext and Validator refer to ValueStack, ParametersInterceptor gets parameters from ActionContext and put them into ValueStack.


<action name="helloWorld" class="com.opensymphony.xwork2.showcase.helloworld.HelloWorldAction">
  <result type="printToConsole">
    <param name="param">${message}</param>
  </result>
</action>
String result = TextParseUtil.translateVariables(param, invocation.getStack());
How to get action's message property inside result? StrutsResultSupport dose this by default?


For Global level, put a 'xwork-conversion.properties' in classpath, use <class name>=<converter class name>
For Action level, put a 'ActionClassName-conversion.properties' in the same folder as action class, use <property name>=<converter class name>
How dose a converter work?


Validator validate an object and put error message in ValidatorContext.
ValidatorSupport's getFieldValue method pushes given object to ValueStack, and calls ValueStack's findValue method.
Put xml in the same folder of action class, use 'ActionClass-validation.xml' for action-level and use 'ActionClass-actionAlias-validation.xml' for action alias-level.


Set system property using UtilTimerStack.ACTIVATE_PROPERTY or UtilTimerStack.MIN_TIME as key, or manipulate on UtilTimerStack to control profiling.


Use AnnotationWorkflowInterceptor and mark @before, @after or @beforeResult in action class's method.


Use XWork-tiger to enable annotation for validation and conversion.


XWork has added some new features to OGNL, including support for ValueStack, statics properties and the "top" keyword.


TextProvider is used to resolve key to messages in properties file by calling it's getText() method, ActionSupport has implemented this interface.


ObjectFactory
Integrate with Spring, use SpringObjectFactory implementation, now the "class" attribute is set to a bean name in spring configuration file, use "singleton"=false.
Use <bean name="default" type="com.opensymphony.xwork2.ObjectFactory" class="com.mycompany.MyObjectFactory" /> to use custom implementation.


Ognl expression is evaluated against ValueStack, when will values be pushed into ValueStack?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值