Wednesday, February 17, 2010

[Spring] Custom PropertyEditors' and <form:select>

I wanted to create a select box for a property of my formbacking object using a custom property editor like in this example. Everything seemed to work fine...
If I selected user and submitted the form, the user object was correctly set in the formbacking object. But whenever the page was reloaded (e.g. due to a validation error in a different field of that form), the select box was not correctly set to the previously chosen user.
After a lot of debuggung and googling I found a post in the SpringSource forum explaining that Spring uses the equals()-method to set the select box to the correct value.
Unfortunately, I hadn't overridden the equals()-method and I was not using the same instances of the user class to fill the select box (via its items attribute) and to set the property of my formbacking object in my custom property editor (via super.setValue(...)).

So, If you ever have the problem of a select box not being set to the correct value, do
  1. override the equals()-method in the class of the property you want to set using or
  2. assure that you are using the same instances of the property's class to fill your as you use to set the value in your custom property editor

No comments:

Post a Comment