According to the JavaDoc for the MultiActionController:
Note: For maximum data binding flexibility, consider direct usage of a ServletRequestDataBinder in your controller method, instead of relying on a declared command argument. This allows for full control over the entire binder setup and usage, including the invocation of Validators and the subsequent evaluation of binding/validation errors.
So, I would suggest the following in your saveCustomer method:
Code:
public ModelAndView saveCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object command = newCommandObject(Customer.class);
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);
if (this.validators != null) {
for (int i = 0; i < this.validators.length; i++) {
if (this.validators[i].supports(command.getClass())) {
ValidationUtils.invokeValidator(this.validators[i], command, binder.getBindingResult());
}
}
}
// Don't save if errors occurred
if(!binder.getBindingResult().hasErrors())
{
...
customerService.saveCustomer(customer);
..
if(errorOccurred)
{
ObjectError error = new ObjectError(getCommandName(command), "<error_message>");
binder.getBindingResult().addError(error);
}
}
ModelAndView modelAndView = new ModelAndView("getCustomer");
// This will add the command object and the errors (along with your custom message) into the model
modelAndView.addAllObjects(binder.getBindingResult().getModel());
return modelAndView;
}