Hi,
I'm having some difficulties with Spring MVC 3 annotated @Controller.
My controller looks like:
The validator is autowired and defined in my application-context.xml.Code:@Controller @RequestMapping("/da/search") public class SearchController { @Autowired @Qualifier("searchFormValidator") private Validator searchFormValidator; @InitBinder("searchForm") public void initBinder(WebDataBinder binder) { // allowed fields (to prevent malicious users posting additional binder.setAllowedFields(SearchForm.getAllowedFields()); // set required fields binder.setRequiredFields(SearchForm.getRequiredFields()); // set date format binding DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false)); // set timeperiod binding binder.registerCustomEditor(TimePeriod.class, new TimePeriodEditor()); // set validator to use for logical errors binder.setValidator(searchFormValidator); } @RequestMapping(method=RequestMethod.GET) public String initSearch(Model model) { SearchForm searchForm = new SearchForm(); searchForm.setPeriod(TimePeriod.LATEST); model.addAttribute("searchForm", searchForm); return "search"; } @RequestMapping(method=RequestMethod.POST) public String performSearch(@ModelAttribute("searchForm") SearchForm searchForm, BindingResult result) { if (result.hasErrors()) { . . } return "search"; }
When I debug I can see that initBinder is called for when I call /da/search.do with HTTP GET. Also, the supports-method of my Validator is called.
When I submit the form (HTTP POST) initBinder is called and also supports but not validate on my validator... shouldn't it be called as well? Or do I have to invoke myself? If so, why would I bother to use the "setValidator" in initBinder-method?


Reply With Quote
