Expert Spring MVC and Web Flow

(Dana P.) #1
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

}

The DataBindersupports nested objects and properties with a simple dot notation, simi-
lar to the JSTL’s EL. An example, contained in Listing 6-10, best illustrates this nesting.

Listing 6-10.NestedCommandBeanTest

public void setUp() throws Exception {
bean = new NestedCommandBean();
binder = new ServletRequestDataBinder(bean, "beanName");
request = new MockHttpServletRequest();
}

public void testSimpleBind() {
// just like /servlet?name.firstName=Anya&name.lastName=Lala
// or name.firstName=Anya&name.lastName=Lala as the payload
// of a POST request
request.addParameter("name.firstName", "Anya");
request.addParameter("name.lastName", "Lala");

binder.bind(request);
assertEquals("Anya", bean.getName().getFirstName()); // true!
assertEquals("Lala", bean.getName().getLastName()); // true!
}

The property name name.firstNameis converted to getName().setFirstName(). The root
bean, in this case a NestedCommandBean, is implicit, and thus it is not mentioned in the binding
string name.
There is no limit to the nesting of objects and properties. Just remember that any object
whose property you are trying to set cannot be null (including collections and objects inside
of collections). The property itself (in this case, firstName) can be null, however.

128 CHAPTER 6 ■THE CONTROLLER MENAGERIE

Free download pdf