The dwr.xmlfile is where we configure which objects are going to be exposed to the
client-side scripts. In our example, we’re exposing the main business object from PetClinic’s
service layer. DWR has several different types of creators. As you can see in the preceding list-
ing, we’re using a Spring creator. This type of creator knows how to obtain a bean by name
from a BeanFactoryor an ApplicationContext. Other types of creator that you might use are
scripted—as with a Bean Scripting Framework–supported script language—and new,where
the object is simply instantiated via reflection.
In the other half of the configuration file, you can see some convertelements too. Not
only does the service object need to be understood by DWR, but any objects that its methods
accept as parameters or objects that they return also have to be capable of conversion. For
Strings and primitives, this is a simple 1:1 mapping to JavaScript equivalents, but for other
objects this has to be specified in configuration. For the PetClinic demo, we simply allow all
of the model beans and the concrete implementation of the service bean to be converted.
■NoteIt would be a security risk to automatically allow conversion of any object for JavaScript use, and
that is the main reason you have to specifically allow conversion on a case-by-case basis in DWR.
DWR is servlet-based; it uses a servlet to dynamically generate much of the JavaScript
code that the client will use. The servlet code is already in dwr.jar, so we just need a declara-
tion and mapping in web.xml. Listing B-2 has the details.
Listing B-2.Changes to WEB-INF/web.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
APPENDIX B ■AJAX AND DWR 379
584X_Ch14_AppB_FINAL 1/30/06 12:55 PM Page 379