views:

59

answers:

1

We are developing an application using Spring MVC. There is a page which displays list of user, a check box next to it, and a submit button at the bottom of the page.

A logged in user can select those check boxes and submit, currently a controller checks whether the selected user list is empty or not and acts accordingly. Should we just bring a validator only to do this check ? or else is it fine to do it in the controller itself ? Is there any doc which says what a controller, validator should do and should not do ?

+1  A: 

Until Spring 3.0 is released - there is no built-in support for model validation. You'll have to handle validation on your own - like this:

@RequestMapping
public String post(@ModelAttribute MyModel myModel, BindingResult result){
   myValidator.validate(myModel, result);
   if (result.hasErrors()) return "myView";
   ...
}

You can do what you like, it's your code. But by convention, the controller should just be concerned with directing things - validation should really be in a separate validator.

Daniel Alexiuc
Thanks Daniel. I could see in your code you explicitly call validate method which is applicable for AbstractCommandController not for SimpleFormController. For SimpleFormController we can just enable the validator in the bean declaration.
novice
Yes you're absolutely right; I've been using annotation-based controllers for so long I forgot about those who extend Spring's controller classes.
Daniel Alexiuc