[Spring] MultiActionController에서 Validator 쓰기

import org.springframework.validation.BindException;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.ServletRequestDataBinder;

...
//MultiAcitionController의 bindObject()를 다음과 같이 override한다.
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;
    }

//Validator를 쓰고 싶은 부분에서,
BindException errors = bindObject(request, command, new XXXValidator());
if (errors.hasError()) {
   mav.addAllObjects(errors.getModel());
   return mav;
}
* mav = ModelAndView
* command = validation 대상
* XXXValidator = 사용자가 만든 Validator 클래스

댓글

Designed by JB FACTORY