Spring 验证器错误代码无法自动解析
Spring validator error code is not auto resolve
消息源配置:
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:locale/normal/message", "classpath:locale/validation/message");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
Spring 验证者:
@Component
public class MyAccountValidator implements Validator {
@Autowired
MyAccountService myAccountService;
@Override
public boolean supports(Class<?> clazz) {
return MyAccount.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
MyAccount myAccount = (MyAccount) target;
MyAccount myAccountDb = myAccountService.findByUserId(myAccount.getUserId());
if (myAccountDb == null) {
errors.reject("myAccount.id.not_found");
}
}
}
message_en.properties:
myAccount.id.not_found=Your id is not found.
休眠验证器:
@NotNull(message="{myAccount.id.not_found}")
private String password;
Spring 控制器:
@RequestMapping(value = { "/validate" }, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.TEXT_HTML_VALUE)
@ResponseBody
public String validate(Model model, @RequestBody @Valid MyAccount myAccount, BindingResult bindingResult, Locale locale) throws JsonProcessingException {
System.out.println("myAccount user id = " + myAccount.getUserId());
String msg = "";
List<ObjectError> errors = bindingResult.getAllErrors();
for (ObjectError error : errors ) {
msg = msg + (error.getObjectName() + " - " + error.getCode() + " - " + error.getDefaultMessage()) + "\n";
}
return msg;
}
在我的控制器中,error.getDefaultMessage()
始终显示 Spring 验证程序错误代码 errors.reject("myAccount.id.not_found");
的空值,无法自动解决。
当我尝试 hibernate 验证器时,正如您在上面的字段注释中看到的那样,它工作正常,@NotNull(message="{myAccount.id.not_found}")
可以在每个语言环境中自动完美解析。
我是否遗漏了任何配置?为什么 Spring 验证器错误代码无法自动运行?
PS: 我的I18N配置应该是正确的,我所有的JSP都能正确显示文字
我找了一整天,可能没有我的问题的答案......我总结了一些发现:
1) 阅读了很多教程,所有教程都在 JSP 视图中显示错误消息,因此他们可能会像这样工作:<form:errors path="*" cssClass="error" element="div"/>
,错误代码将得到解决语言环境消息自动。但就我而言,我想通过 ajax 获取错误消息,因此,要解决控制器中的错误代码,我必须手动执行,不幸的是我必须注入 messagesource
和 locale
在我的控制器中:
@RequestMapping(value = { "/validate" }, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.TEXT_HTML_VALUE)
@ResponseBody
public String validate(Model model, @RequestBody @Valid MyAccount myAccount, BindingResult bindingResult, Locale locale) throws JsonProcessingException {
System.out.println("myAccount user id = " + myAccount.getUserId());
String msg = "";
List<ObjectError> errors = bindingResult.getAllErrors();
for (ObjectError error : errors ) {
msg = msg + (messageSource.getMessage(error, locale)) + "\n";
}
return msg;
}
2) 阅读Resolving codes to error messages,解析器是将错误代码解析为一个或多个消息代码,它无助于将消息代码映射到messagesource
。我误解了 error code
和 message code
之间的术语。
如果有人有解决办法,请分享...
消息源配置:
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:locale/normal/message", "classpath:locale/validation/message");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
Spring 验证者:
@Component
public class MyAccountValidator implements Validator {
@Autowired
MyAccountService myAccountService;
@Override
public boolean supports(Class<?> clazz) {
return MyAccount.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
MyAccount myAccount = (MyAccount) target;
MyAccount myAccountDb = myAccountService.findByUserId(myAccount.getUserId());
if (myAccountDb == null) {
errors.reject("myAccount.id.not_found");
}
}
}
message_en.properties:
myAccount.id.not_found=Your id is not found.
休眠验证器:
@NotNull(message="{myAccount.id.not_found}")
private String password;
Spring 控制器:
@RequestMapping(value = { "/validate" }, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.TEXT_HTML_VALUE)
@ResponseBody
public String validate(Model model, @RequestBody @Valid MyAccount myAccount, BindingResult bindingResult, Locale locale) throws JsonProcessingException {
System.out.println("myAccount user id = " + myAccount.getUserId());
String msg = "";
List<ObjectError> errors = bindingResult.getAllErrors();
for (ObjectError error : errors ) {
msg = msg + (error.getObjectName() + " - " + error.getCode() + " - " + error.getDefaultMessage()) + "\n";
}
return msg;
}
在我的控制器中,error.getDefaultMessage()
始终显示 Spring 验证程序错误代码 errors.reject("myAccount.id.not_found");
的空值,无法自动解决。
当我尝试 hibernate 验证器时,正如您在上面的字段注释中看到的那样,它工作正常,@NotNull(message="{myAccount.id.not_found}")
可以在每个语言环境中自动完美解析。
我是否遗漏了任何配置?为什么 Spring 验证器错误代码无法自动运行?
PS: 我的I18N配置应该是正确的,我所有的JSP都能正确显示文字
我找了一整天,可能没有我的问题的答案......我总结了一些发现:
1) 阅读了很多教程,所有教程都在 JSP 视图中显示错误消息,因此他们可能会像这样工作:<form:errors path="*" cssClass="error" element="div"/>
,错误代码将得到解决语言环境消息自动。但就我而言,我想通过 ajax 获取错误消息,因此,要解决控制器中的错误代码,我必须手动执行,不幸的是我必须注入 messagesource
和 locale
在我的控制器中:
@RequestMapping(value = { "/validate" }, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.TEXT_HTML_VALUE)
@ResponseBody
public String validate(Model model, @RequestBody @Valid MyAccount myAccount, BindingResult bindingResult, Locale locale) throws JsonProcessingException {
System.out.println("myAccount user id = " + myAccount.getUserId());
String msg = "";
List<ObjectError> errors = bindingResult.getAllErrors();
for (ObjectError error : errors ) {
msg = msg + (messageSource.getMessage(error, locale)) + "\n";
}
return msg;
}
2) 阅读Resolving codes to error messages,解析器是将错误代码解析为一个或多个消息代码,它无助于将消息代码映射到messagesource
。我误解了 error code
和 message code
之间的术语。
如果有人有解决办法,请分享...