Spring MVC: Null or an Empty String?
Wondered why Spring MVC converts an empty field to an empy string instead of null?
Spring MVC converts your empty fields to an empty string instead of null by default. This is often not what you want when you persist your entities. The reason for this behavior is that Spring MVC uses Java's default property editor for strings when binding the request to your domain object.
A better alternative is to use the StringTrimmerEditor that removes surrounding white space from the field values and optionally converts an empty value to null instead of an empty string.
Just override the initBinder method of your SimpleFormController:
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception
{
// bind empty strings as null
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
References