OverviewDWR is a powerful tool allows Javascript in a browser to interact with Java on a server and helps you manipulate web pages with the results. WebWork integrates with DWR to allow WebWork's action to be invoked through XHR (Ajax) and the results / action made available at the client side (browser).
ConfigurationTo configure DWR integration with WebWork, first we need to set up web.xml <servlet> <servlet-name>dwr</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>org.directwebremoting.extend.AccessControl</param-name> <param-value>com.opensymphony.webwork.dwr.WebWorkDwrAccessControl</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
Then we need to configure dwr.xml, which typically resides under /WEB-INF) <!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr//dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="validator">
<param name="class" value="com.opensymphony.webwork.validators.DWRValidator"/>
</create>
<convert converter="bean" match="com.opensymphony.xwork.ValidationAwareSupport"/>
<create creator="new" javascript="dwraction">
<param name="class" value="org.directwebremoting.webwork.DWRAction" />
</create>
<convert converter="bean" match="org.directwebremoting.webwork.ActionDefinition"/>
<convert converter="bean" match="org.directwebremoting.webwork.AjaxResult" />
<convert converter="bean" match="com.opensymphony.xwork.ActionSupport" />
</allow>
<signatures>
<![CDATA[
import java.util.Map;
import com.opensymphony.webwork.validators.DWRValidator;
import org.directwebremoting.webwork.ActionDefinition;
import org.directwebremoting.webwork.DWRAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
DWRValidator.doPost(String, String, Map<String, String>);
DWRAction.execute(ActionDefinition, Map<String, String>, HttpServletRequest, HttpServletResponse, ServletContext);
]]>
</signatures>
</dwr>
ConceptThe concept of WebWork/DWR integration is to allow :-
UsageWe need to declare the following script for DWR / WebWork integration to work
<script src='<ww:url value="/dwr/interface/dwraction.js" />'></script> <script src='<ww:url value="/dwr/engine.js" />'></script> <script src='<ww:url value="/dwr/util.js" />'></script> Invoke WebWork's Action and execute the resultTo invoke WebWork's action and execute its result we'd do
dwraction.execute( // An Javascript Object that will be converted to ActionDefinition by DWR { action: 'myWebWorkAction', // our webwork action name namespace: '/myNamespace', // our webwork namespace executeResult: true // let's execute our result as well }, // Parameters we'd like to passed to WebWork's Action { param1: 'value1', param2: 'value2' }, // our callback function function(ajaxResult) { // ajaxResult will be DefaultAjaxTextResult (converted to js object by DWR) // the executed result (String) will be stored in ajaxResult.text document.getElementById("ourDivId").innerHTML = ajaxResult.text; } ); Invoke WebWork's Action but don't execute its result, instead expose its Actiondwraction.execute( // An Javascript Object that will be converted to ActionDefinition by DWR { action: 'myWebWorkAction', // our webwork action name namespace: '/myNamespace', // our webwork namespace executeResult: false // let's NOT execute our result as well }, // Parameters we'd like to passed to WebWork's Action { param1: 'value1', param2: 'value2' }, // our callback function function(ajaxResult) { // ajaxResult will be DefaultAjaxDataResult (converted to js object by DWR) // the action will be exposed as ajaxResult.data // Let's say for example, we have a getter for MyString in our WebWork action, we could do // the following to retrieve it document.getElementById("ourDivId").innerHTML = ajaxResult.data.myString; } ); ExampleThere's an example in WebWork's showcase under the section WebWork DWR integration. Might want to check it out as well. DebuggingWe could turn on debugging as in this example (through debug init-param web.xml), and access http://host:port/context/dwr/
To see if dwraction interface is exposed properly without any warnings or errors. |