Expert Spring MVC and Web Flow

(Dana P.) #1
Hopefully you’re starting to see where this is leading now, and you’ve probably guessed
correctly that DWR can expose the objects from your Spring ApplicationContextdirectlyto
the JavaScript functions in your web pages. In fact, DWR even has support for Spring built in—
making this task even easier and one of the reasons we choose to highlight it.

A Practical Example


Enough of the history and theory; let’s see some code! In order to demonstrate the concepts
and a couple of the gotchas, we’ve taken one of the Spring sample applications (PetClinic,
shipped with the Spring distribution) and enhanced it. We’ll step through those changes that
Ajax-enabled our application.

■Tip If you’d like to hack along or prefer to just dive in, you can download a WARfile of the modified
PetClinic application from the Spring wiki at http://opensource.atlassian.com/confluence/
spring/x/Yws.

Configuration and Code Changes


First, we added dwr,jar from the DWR download to the WEB-INF/libdirectory. We also
created a new file called WEB-INF/dwr.xml. Listing B-1 shows the complete dwr.xmlfile.

Listing B-1.WEB-INF/dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">

<dwr>
<allow>
<create creator="spring" javascript="Clinic" scope="session">
<param name="beanName" value="clinic"/>
</create>
<convert
converter="bean"
match="org.springframework.samples.petclinic.*"/>
<convert
converter="bean"
match="org.springframework.samples.petclinic.jdbc.*"/>
</allow>
</dwr>

378 APPENDIX B ■AJAX AND DWR

584X_Ch14_AppB_FINAL 1/30/06 12:55 PM Page 378

Free download pdf