Programming Tutorials

Converting properties using PropertyEditors and Other Spring features worth mentioning

By: Saravanan in Java Tutorials on 2010-01-01  

In this tutorial we are going to see how to Convert properties using PropertyEditors and Other Spring frame work features worth mentioning. This is handy to be able to represent properties in a different way than the object itself.

For example, a date can be represented easily readable, while we're still able to convert the easily readable form back to the original date. This behavior can be achieved by registering custom editors, of type java.beans.PropertyEditor. Registering custom editors to a BeanWrapper gives it the knowledge of how to convert properties to the desired type. An example of working with a PropertyEditor converting java.util.Date objects to an easily readable form.

public class MainClass 
public void setBirthDay(Date d);
public Date getBirthDay();

public void doIt() 
SimpleDateFormat d = new SimpleDateFormat("dd-MM-yyyy");
CustomDateEditor e = new CustomDateEditor(d, false);
MainClass m = new MainClass();
BeanWrapper b = new BeanWrapper(m);
bw.setPropertyValue("birthDay", "12-12-1950");

The notion of PropertyEditors is quite important to for instance the MVC framework, but also other parts of the framework, so in the rest of this document, sometimes  references to this part occur.  Other features worth mentioning:

  • Determining readability and writability: Using the isReadable() and isWritable() methods, we can determine whether or not a property is readable or writable
  • Retrieving propertydescriptors: Using getPropertyDescriptor(String) and getPropertyDescriptors() we can retrieve objects of type java.beans.PropertyDescriptor, that might come in handy sometimes

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)