validation in multiaction controller
Hi guys I have successfully implemented validation in multiaction controller .actually i have tweaked the bind method and defined bindObject method in a super class.BaseController which extends MultiactionController.
protected BindException bindObject(HttpServletRequest request, Object command,
Validator validator) throws Exception {
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);
BindException errors = new BindException(command,getCommandName(command));
if(validator.supports(command.getClass())){
ValidationUtils.invokeValidator(validator, command, errors);
}
return errors;
}
and multiaction class extends this class and in any method for example authenticateUser method ---->
public ModelAndView authenticateUser(HttpServletRequest request,
HttpServletResponse response,Login command) throws Exception {
System.out.println("Authenticating user : ");
ModelAndView mv = new ModelAndView("welcome","command",command);
mv.addObject("command",command);
BindException errors = super.bindObject(request, command, new LoginValidator());
if(errors.hasErrors()) {
mv.addAllObjects(errors.getModel());
mv.setViewName("login");
return mv;
}
return mv;
}
Here is the bind method in multiaction controller
protected void bind(HttpServletRequest request, Object command) throws Exception {
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);
}
and in the loginValidator --
public void validate(Object target, Errors errors) {
Login login = (Login) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userId", "error.not-specified", "field can not be empty");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "error.not-specified", "field can not be empty");
}
it works fine if u have defined the properties file to show the errors in jsp using <form:errors path> and command object is Login with userId and password properties