文章目录
import org.springframework.validation.BindingResult;
@RequestMapping(value = "add", method = RequestMethod.POST)
public BaseResponse add(HttpServletRequest request, @ModelAttribute @Valid User user, BindingResult errors) throws UnsupportedEncodingException {
if (errors.hasErrors()) {
return buildError(errors.getFieldErrors());
}
return buildOK();
}
public BaseResponse buildAjaxError(List<FieldError> fieldErrors){
FormResponse result = new FormResponse();
result.setResponseMessage(Resp._PARAM_ERROR);
for (FieldError fieldError : fieldErrors){
result.setFieldErrorMsg(fieldError.getField(), fieldError.getDefaultMessage());
break;
}
return result;
}