代码如下:
....
@RequestMapping( "/test")
@ResponseBody
public ResponseTO test(
@Valid @RequestBody RequestTO to, HttpServletResponse resp)
{
//how do I get validation results here?
return new ResponseTO("111");
}
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new TestValidator());
}
class TestValidator implements Validator {
/**
* This Validator validates just Person instances
*/
public boolean supports(Class clazz) {
return RequestTO.class.equals(clazz);
}
public void validate(Object obj, Errors e) {
ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
RequestTO tx = (RequestTO) obj;
if (tx.getName().equals("buy")) {
e.rejectValue("feature", "NOT BUY");
}
}
}
我无法弄清楚的部分是如何在控制器中获得验证结果?谢谢!