Introduction to WebWork action invocation through DWR
From WebWork 2.2.7, DWR integration is included with Webwork. You can find out more from the documentation on the Webwork Wiki.
WebWork support in DWR
There are a couple of things that you must do in order to be able to use WW actions through DWR.
Step 1: Configure dwr.xml
You must include in the dwr configuration file:
<create creator="none" javascript="DWRAction"> <param name="class" value="org.directwebremoting.webwork.DWRAction"/> <include method="execute"/> </create> <convert converter="bean" match="org.directwebremoting.webwork.ActionDefinition"> <param name="include" value="namespace,action,method,executeResult" /> </convert> <convert converter="bean" match="org.directwebremoting.webwork.AjaxResult"/>
In case your AJAX WebWork action invocations return action instances (and not pure text), than you must include converter definitions for your action objects (package level or for individual actions).
<convert converter="bean" match="<your_action_package>.*"/>
Step 2: Import scripts in your JSP files
Follow the rules to enable DWR in your JSP actions. Also include the DWRActionUtil.js script (place it in your scripts web directory).
Usage
Invoking an action from JS is done using the following:
DWRActionUtil.execute(id, params, callback [, displayMessage]);
Where id is one of:
- actionUri: the action URI to be called (WITHOUT .action). For example:
DWRActionUtil.execute('/ajax/TestFM', 'myform', 'doOnTextResult'); - actionDefinitionObject: an object that specifies an action from
xwork.xml. It must specify the following fields:- namespace: the action namespace according to xwork.xml
- action: the action name according to xwork.xml
- executeResult: true|false (execute the result of the action, or if false return the action instance)
E.g.:
DWRActionUtil.execute({
namespace:'/ajax',
action:'TestJS',
executeResult:'true'
}, 'data', doOnJSResult, "stream...");
params must be one of:
- emptyParams: Pass
{}to ignore any parameters.Example:
DWRActionUtil.execute('/ajax/TestFM', {}, doOnJSResult, "stream..."); - fieldId: id of a field whose value will be transmitted as an action invocation parameter.
Example:
<input id="mytext" name="mytext" value="some value" type="text"/> DWRActionUtil.execute('/ajax/TestFM', 'mytext', doOnJSResult, "stream..."); - formId: the id of a form. All input values will be transmitted as action invocation parameters
Note: If your action configuration in xwork.xml uses the parameters interceptor than your action will be initialized correctly with the parameters values. For more documentation, please consult WebWork available documentation.
And callback is either:
- callbackFunction: as in DWR, the function to be invoked upon request completion.
- callbackObject: as in DWR, the callback object.
Finally displayMessage is an optional parameter which specifies a message to be displayed till request completion (see DWR documentation)
Advanced
You can declare a pre/post action processor by providing in web.xml through a context-wide initialization parameter (dwrActionProcessor). The processor must implement org.directwebremoting.webwork.IDWRActionProcessor interface. The processor will be invoked before and after the action has been invoked, so that you can prepare the initial invocation or change the result.