I am using Spring validation(JSR 303) in one of the web apps.I have no issues when a user submits data and spring validation works pretty neat.But I have a scenario where I have to fetch data from a service and validate it and then bind them to my view.(something non-form validation).How can I use @Valid in this case or does it have to be done differently?
Here is a sample code,i started with
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ModelAndView getView(
@PathVariable("id") final String id, @User user,
HttpSession session) {
User user= getUser();
BindingResult result = new BeanPropertyBindingResult(user, "user");
validator.validate(user, result);
if(result.hasErrors()){
logger.log(Level.ERROR, "Errors");
}
ModelAndView view = new ModelAndView ("home");
view.addObject("user",user );
view.addAllObject(result.getModel());
return view;
As far as I understand you need to inject default
org.springframework.validation.Validatorinto your controller (if@Validworks you should be able to do it)run validation manually as follows
and merge results into
ModelMap(declare it as argument of your method) as follows