如何检测Spring Validation是否通过?

时间:2012-10-20 04:17:09

标签: spring hibernate bean-validation

我正在尝试使用注释来使用Spring 3.0验证。 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html#core-convert

我的代码在文档中看起来像:

@Controller
public class MyController {

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
       binder.setValidator(new FooValidator());
    }

    @RequestMapping("/foo", method=RequestMethod.POST)
    public void processFoo(@Valid Foo foo) { ... }

}

public class PersonValidator implements Validator {

    /**
     * This Validator validates just Person instances
     */
   public boolean supports(Class clazz) {
    return Person.class.equals(clazz);
  }

public void validate(Object obj, Errors e) {
    ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
}

}

如果验证通过了,我如何在RequestMapping方法中告知?

1 个答案:

答案 0 :(得分:0)

Aah ......这很简单......只是想通过我的RequestMapping中有一个BindingResult参数,它有一个hasErrors()方法。