Using Customer PropertyEditor for Form Data Binding

 

When working with Spring forms, we normally bind the form properties into a command class. However, there would be some troubles if your command class has some properties that are not String, as all values from the form are passed as String. Therefore, you would expect some facilities to handle the conversion and this is what would be discussed in this article

 

The SimpleFormController inherits initBinder() method that allows you to register custom editors for certain fields of your command class. For instance, you will be able to transform Date objects into a String pattern and back, in order to allow your JavaBeans to have Date properties and still be able to set and display them in an HTML interface.

 

public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {

      binder.registerCustomEditor(Date.class,

new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), false));

                    

}

 

DataBinder that allows for setting property values onto a target object, including support for validation and binding result analysis. The binding process can be customized through specifying allowed fields, required fields, custom editors, etc. For example, like in the preceding code, the binder allows you to use the CustomDateEditor to covert/reset the value of any Date object that bound in the form. The CustomDateEditor implements PropertyEditorSupport which has two default methods: The setAsText(String text) Parse the Date from the given text, using the specified DateFormat; getAsText() formats the Date as String, using the specified DateFormat.

 

CustomDateEditor is not really a customer property editor as it is actually from spring propertyeditors package. You can write your own one just like CustomDateEditor. The simplest usage is to overwrite the setAsText(String text) and getAsText() methods. For example:

 

//bind your MetadataPropertyEditor to metadata object

binder.registerCustomEditor(Metadata.class, new MetadataPropertyEditor());

 

//overwrite the methods

    public void setAsText(String text) throws IllegalArgumentException {

        Metadata metadata = new Metadata();

        metadata.setMetadataId(Long.valueOf(text));

        setValue(metadata);

    }

 

    public String getAsText() {

        Metadata metadata= (Metadata) getValue();

        if (metadata!=null) {

            return metadata.getMetadataId().toString();

        }

        return "";

    }

Advertisements
This entry was posted in Spring. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s