Binding

Default Ognl-base binding

By default all binding of fields to Controllers is handled by Ognl. Waffle's OgnlDataBinder allows for the injection of a ValueConverterFinder, which by default is only configured to use OgnlValueConverter, but can be customized to use any number of Waffle ValueConverters. The OgnlValueConverter allows fields of standard types (e.g. String, Number, primitives) to be bound to your controllers automatically.

A common binding problem that many web application need to deal with is how to bind a String value to a Date object. Of course each application and locale has it's own unique format. As an example we will build a class that supports binding to a Date. From this example you'll gain an understanding of how to bind to any Class type.

Suppose we have the following Controller class ControllerWithDateField which has one field startDate which is of course a java.util.Date:

You could imagine that the request to set this date field would be something similar to startDate=04-07-2006. So inorder to bind this to the underlying Controller we will need to create a custom class that will handle conversion for a specific type(s).

Custom binding using ValueConverters

Implementation of the ValueConverter interface is needed to handle these custom value conversions. This interface defines two methods:

Method Description
boolean accept(Class type) determine whether this implementation can handle conversions for the class type passed.
Object convertValue(String propertyName, String value, Class type) responsible for handling the conversion (only called if implementation returned true from the accept() method.

Nothing clarifies a description better than an example so lets look at the implementation Waffle provides for handling Date types:

Now all that is left is to register this converter within the web.xml