jackyesind
jackyesind

Reputation: 3373

Form values to be NULL instead of empty strings in Spring MVC

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

Answers (1)

muthu
muthu

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));  
}

Source

Upvotes: 33

Related Questions