Reputation: 3373
I am using Spring MVC. I have a form on my page. When I do not enter any values to the form and submit it, it comes with empty strings ""
.
Is it possible to set null
value instead of empty strings?
Thanks in advance for your help.
Upvotes: 21
Views: 9542
Reputation: 5461
You have to use the @InitBinder annotation because in Spring MVC it always returns ""
for the blank values in a form. You have to add this in your controller.
Example:
@InitBinder /* Converts empty strings into null when a form is submitted */
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
Upvotes: 33