Expert Spring MVC and Web Flow

(Dana P.) #1
Listing B-3.The Refactored Entity Class

package org.springframework.samples.petclinic;

public class Entity {

private Integer id;

public void setId(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public boolean isNewEntity() {
return (this.id == null);
}
}

Bean property names are converted to JavaScript properties by default, so isNew()would
become just new()on the JavaScript object, which is a reserved name. Using an IDE such as
Eclipse (with a little manual search and replace in the JSP files!), we were quickly able to man-
age all of the dependencies of such a change. That wraps it up for the configuration and code
changes—all in all, a fairly simple exercise to set the application up for DWR.

Presentation File Changes


Now for the interesting bit: We added two files to the root of the PetClinic web application—
index.htmland local.js—which contain the application-specific functions to complement
the DWR-provided ones. In the <head>section of the HTML file, we add references to all of the
script libraries required, as shown in Listing B-4.

Listing B-4.Script Library References

<head>
...
<script type='text/javascript' src='/petclinic/dwr/interface/Clinic.js'></script>
<script type='text/javascript' src='/petclinic/dwr/engine.js'></script>
<script type='text/javascript' src='/petclinic/dwr/util.js'></script>
<script type='text/javascript' src='/petclinic/local.js'></script>
</head>

At the bottom is the file we code ourselves, which we’ll look at shortly. The other three
are supplied by DWR. engine.jsand util.jsare fairly static and contain the bulk of the DWR
functionality. clinic.jsis the interesting one: the JavaScript representation of the service
object that we exported in our WEB-INF/dwr.xmlfile.

APPENDIX B ■AJAX AND DWR 381

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

Free download pdf