JavaScud | Forum | JIRA | Blog |
  Dashboard > WebWork2文档中文化计划 > ... > XWork Configuration > Action configuration
  WebWork2文档中文化计划 Log In View a printable version of the current page.  
  Action configuration
Added by 黑灵, last edited by scud on May 18, 2006  (view change)
Labels: 
(None)

Action是Webwork的基础"工作单元".一个action一般就是一个请求(或点击按钮或提交表单).action元素(tag就和JSP太同义了)有两部分,一个友好的名字(URL相关,如saveForm.action)和一个负责"处理"的类.

<action name="formTest" class="com.opensymphony.webwork.example.FormAction" method="processForm">

可选属性"method"用来告诉WebWork调用action的那个方法.如果method属性为空,WebWork调用默认调用*execute*()方法.如果Action类中既没有execute()方法也没有在xml文件中指定其他方法,WebWork会抛出异常.

您也可以在您的表单中用"actionName!something"的方式告诉WebWork调用Action类中的"something"方法.例如"formTest!save.action"会调用FormAction类中的"save"方法.这个方法必须是public且没有参数.

public String save() throws Exception{
...
return SUCCESS;
}

"actionName"的所有配置包括拦截器和返回类型等都会被"actionName!something"使用.

Action Support

Action的类属性可以像下面一样省略:

<action name="myAction">
   ....
</action>

在这种情况下,会缺省使用com.opensymphony.xwork.ActionSupport 类,它有一个execute()方法,缺省返回SUCCESS.

默认Action引用

从Webwork2.2.1开始您也可以指定一个当xwork.xml中找不到指定的action时执行的默认action.这一特性主要是用来满足为创建非常简单或相似的action类或元素的需求.默认action名可以在package元素里面这样配置:

<package name="myPackage" ....>

...

<default-action-ref name="simpleViewResultAction">

<!-- 
An example of a default action that is just a simple class 
that has 3 fields: successUrl, errorUrl, and inputUrl. This action 
parses the request url to set the result values. In the normal case 
it just renders velocity results of the same name as the requested url.
-->
<action name="simpleViewResultAction" class="SimpleViewResultAction">
<result type="velocity">${successUrl}</result>
<result name="error" type="velocity">${errorUrl}</result>
<result name="input" type="velocity">${inputUrl}</result>
</action>

... 

</package>
当心

应该保证在每个名称空间中只有一个默认action被配置.因此如果你在同一个空间里配置了多个默认aciton,系统就不知道那个是默认的了.

注意

注意第一个result的属性省略了,WebWork缺省会把它当作"success".

在这种情况下,如果请求到action的映射没有在这个包里定义,它会自动转向到别名为 "simpleViewResultAction" 的action来执行.

这里的大多数内容都是 Matt Dowell <matt.dowell@notiva.com> 提供的.

Site running on a free Atlassian Confluence Open Source Project License granted to WebWork China. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.3 Build:#808 May 29, 2007) - Bug/feature request - Contact Administrators