I have a command object that represents a form object submitted. If a value from a form input field is blank, I want it to be null
in the command object.
I read a different SO question that showed to use the InitBinder
I gave that a shot, but the initBinder
method is never called.
My controller
package com.example;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import com.example.MyDTO;
@Controller
public class MyControllerImpl implements MyController {
@InitBinder("myControllerImpl")
public void initBinder(WebDataBinder binder) {
System.out.println("init binder");
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
@Override
public String someAction( MyDTO someDTO ) {
System.out.println(someDTO);
// Do something with DTO. I want blank values to be null here
return "somePage;
}
}
DTO
package com.example;
public class MyDTO {
private String name;
public String getName() {
return name;
}
public String setName() {
return name;
}
}
I also tried adding the class name to the InitBinder
annotation
@InitBinder("myControllerImpl")
but it did not work.
What am I missing to get the init binder to be called?
I'm using Java 6 and Spring 3