Data Binding

In addition to the simpler controllers such as the ones we discussed just before, Spring features some advanced data binding controllers. Using those, you can model form workflow in your web applications and other interactions that require binding of request parameters to objects specified up front. To explain the concept, let's look at an example. Consider the following request:   show=The+Lion+King&

Data binding will allow us to "bind" the request parameters (show and email) to the following command object:

package org.springframework.prospring.sample.actions.SendInfo;     public class SendInfo {       private Show show;   private String email;       public void setShow(Show show) { = show;   }       public void setEmail(String email) { = email;   } }

The incoming request contains a movie title and an email address of someone a user thinks is interested in the movie. Using the SendInfo object, we can easily send the person an email. The request contains all information already, so the only thing we need to do is actually map the incoming information onto a SendInfo object and take care of the emailing (for which, of course, we've got another component wired up). The mapping is taken care of by Spring's data binding controllers.


Spring won't limit you to binding data of only primitives or framework-specific types. Complete freedom to bind to a complex domain model will be maintained without forcing you to implement specific superclasses or interfaces. Most of this is done using PropertyEditors, discussed in Chapters 2 and 3. Custom binding is also possible. We'll discuss this later.

In Chapters 2 and 3 you learned about PropertyEditors and validation. Those concepts are heavily used when leveraging data binding with Spring, not only when using the web framework. Please familiarize yourself with those concepts if necessary before reading on.

We won't explain each and every data-binding controller in detail. First of all, you'll learn about some special features Spring includes to ease the pain of working with forms and binding parameters to requests and validation data. After that, we'll have a look at some use cases that will show you how to use the Spring MVC framework in combination with some of the components we've just described and functionality you've already seen (reserving a seat, for example).

Convenient Functionality When Working with Data Binding

You learned the basics about data binding in the previous section. Also, you've seen the notion of a Validator earlier in this book. This section discusses the features Spring includes for working with those two concepts in the web tier. We'll discuss possible problems you might encounter when working with checkboxes, the solutions Spring provides, and also cover indexed and mapped properties.

Checkboxes and Radio Buttons

Checkboxes have a nasty habit of sometimes getting lost in translation because of a quirk in HTML. When a checkbox in an HTML page is not checked, its value will not be sent to the server as part of the HTTP request parameters once the form is submitted. Most of the time you do want your domain objects to reflect the state of the unchecked checkbox. Lists modeled using radio buttons have the same behavior. Spring has support for resetting unchecked checkboxes, even though these are not communicated back to the server.

To tell Spring to set the value of a checkbox to false if no parameter is found in the request for the specific checkbox (in other words, if the checkbox is unchecked), include an extra (best left hidden) parameter prefixed by an underscore ("_"). By doing this, you're saying: "The checkbox was visible in the form and I want my object that the form data will be bound to, to reflect the state of the checkbox no matter what!"

It's possible to remove this behavior if you need to. In the initBinder() method of your controller(s), simply set the fieldMarkerPrefix property of the ServletRequestDataBinder to null.

Indexed and Mapped Properties: Maps, Lists, and Arrays

Spring has simple support for binding indexed properties on collections such as arrays, lists, and maps. As much as possible, the support conforms to existing notations such as the ones used by the JSTL Expression Language (EL) and the Object Graph Navigation Language (OGNL). The table that follows offers a list of examples of how you would bind properties to a collection of objects of the following type:

public class Person {   private String name;   public String getName() { return; }   public void setName(String name) { = name; } }




Map (existing in the model under the name map), containing String mapped to Person objects




All three notations are valid; k1 in this case is the key under which the object in question is stored

Array or List containing Person objects, stored in the model under the name array




0, 1, and 2 are the (obviously zero-based) indices of the elements in the list

Professional Java Development with the Spring Framework
Professional Java Development with the Spring Framework
ISBN: 0764574833
EAN: 2147483647
Year: 2003
Pages: 188 © 2008-2017.
If you may any questions please contact us: