Expert Spring MVC and Web Flow

(Dana P.) #1
}

public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

}


The Nameclass comes straight from a domain object model, as it is a simple plain old Java
object (POJO). We wish to create an instance of this class when a form is submitted and popu-
late it from form fields. Listing 6-6 contains the example HTML form with fields that
correspond to our NamePOJO.


Listing 6-6.CommandBean HTML Form




First Name:



Last Name:




We see the first requirement when using the DataBinderframework here in the form.
The form field names match the property names of the Nameclass. More specifically, the form
field names match the JavaBean translation of the Namegetters and setters. For example, the
method setFirstName()is converted via JavaBean semantics to “the setter for the firstName
property.” The DataBinderperforms this conversion from getter and setter methods to prop-
erty names so that it can match the form fields from the HTTP request.


■CautionWhen using the DataBinder,the bean that you are binding to must have a public setter for the
property. If the bean is missing the setter or if it is spelled differently, no error is generated, and the property
will not be set. It is also important to have a public getter for the property, so that the field may be retrieved
by the view. The DataBindercannot perform direct field access; it must go through setters or getters.


To demonstrate how simple the binding process is, we have created a JUnit TestCase
(contained in Listing 6-7) that binds the parameters of a HttpServletRequestto a JavaBean of
type Name. We are isolating the actual binding here, so that you may get a clear picture of how a
bean’s properties are populated. Know that this is all hidden from you when working with
BaseCommandControllerand its subclasses.


CHAPTER 6 ■THE CONTROLLER MENAGERIE 125
Free download pdf