Expert Spring MVC and Web Flow

(Dana P.) #1

Non-default PropertyEditors


Some types of classes can’t be created from Strings without context specific configurations. For
instance, there are many different valid text representations of a java.util.Date, so it is impracti-
cal to provide a default Date PropertyEditorto handle all the different cases. To allow you to
control the format used for conversion, Spring’s Date PropertyEditor, the CustomDateEditor,
allows you to define the format you expect the date to be entered as. Once configured, you
simply register the PropertyEditorwith the DataBinder.
For example, we will create a simple command bean (shown in Listing 6-25) with a
single property of type java.util.Date. We then create an HTML form (Listing 6-26) that requests
the user enter a date with the format YYYY-MM-DD, as a single String(e.g., 2005-03-21). We will
register a CustomDateEditorto handle the conversion, delegating to a java.text.SimpleDateFormat
class.


Listing 6-25.DateCommandBean Class


public class DateCommandBean {


private Date date;

public Date getDate () {
return dateProperty;
}

public void setDate (Date date) {
this.date = date;
}

}


Listing 6-26.DateCommandBean HTML Form




Date: (YYYY-MM-DD)




For this example, we will configure the CustomDateEditorto use the text format yyyy-MM-dd,
as defined by the java.text.SimpleDateFormatclass. Listing 6-27 illustrates these concepts
together.


CHAPTER 6 ■THE CONTROLLER MENAGERIE 139
Free download pdf