我,我需要同時使用彈簧驗證和JSR-303驗證用於驗證的ModelAttribute的使用情況。驗證完美。從我的自定義Spring Validator中引發的錯誤正在被充分翻譯。另一方面,JSR-303s是可行的(錯誤被提出並且是綁定結果的一部分),但是代碼沒有被翻譯。事實上,我看到了代碼本身。
問題示例: 將一個字段留空(我的自定義驗證程序在下面覆蓋),另一個字段爲空(由JSR-303註釋覆蓋)。
輸出在JSP如下:
- error.applicationContactEmailAddress.required(在JSR-303沒有正確翻譯錯誤)
- 請輸入法的主頁警告稱號。 (這是正確的翻譯我的自定義驗證錯誤信息)
AppConfigController.java
public class AppConfigController {
@Autowired
private Validator jsr303Validator;
@Autowired
private AppConfigValidator appConfigValidator;
@InitBinder("appConfig")
protected void initBinder(WebDataBinder binder) {
binder.addValidators(jsr303Validator, appConfigValidator);
binder.setDisallowedFields("id");
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String processUpdate(@Valid @ModelAttribute AppConfig appConfig,
BindingResult result,
SessionStatus status) {
if(result.hasErrors()) {
return UPDATE_VIEW;
} else {
...
status.setComplete();
return SUCCESS_REDIRECT;
}
} else {
return "common/error";
}
}
}
AppConfigValidator.java
@Component
public class AppConfigValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return AppConfig.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
AppConfig appConfig = (AppConfig) target;
if (appConfig.isHomePageWarningEnabled()) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "homePageWarningTitleFr", "error.homePageWarningTitleFr.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "homePageWarningTitleEn", "error.homePageWarningTitleEn.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "homePageWarningFr", "error.homePageWarningFr.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "homePageWarningEn", "error.homePageWarningEn.required");
}
}
}
AppConfig.java
public class AppConfig extends BaseEntity implements Serializable {
...
@NotEmpty(message = "error.applicationContactEmailAddress.required")
@Email(message = "error.applicationContactEmailAddress.invalid")
private String applicationContactEmailAddress = "";
...
}
我也得到了與問題,如自定義的約束:
@Hostname(message = "error.smtpServerHostname.invalid")
WebConfig.java
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
String[] baseNames = {
"resources.messages.layout.LayoutResources",
"resources.messages.layout.MenuResources",
"resources.messages.option.AppConfigResources",
"resources.messages.global.GlobalResources",
"resources.messages.contact.ContactResources",
"resources.messages.currentsession.CurrentSessionResources",
"resources.messages.welcome.WelcomeResources",
"resources.messages.user.UserResources",
"resources.messages.role.RoleResources",
"resources.messages.profile.ProfileResources"
};
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
messageSource.setBasenames(baseNames);
return messageSource;
}
@Bean
public LocalValidatorFactoryBean jsr303Validator() {
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.setValidationMessageSource(this.messageSource());
return localValidatorFactoryBean;
}
定製JSP標記(如此顯示在JSP誤差總是一襯墊)
<spring:hasBindErrors name="${modelAttribute}">
<section id="errors-placeholder" class="alert alert-danger">
<h3><spring:message code="header.errors" /></h3>
<p>
<ul>
<spring:bind path="${modelAttribute}.*">
<c:forEach var="error" items="${status.errors.allErrors}">
<li><spring:message message="${error}"></spring:message></li>
</c:forEach>
</spring:bind>
</ul>
</p>
</section>
在JSP
<t:errors modelAttribute="appConfig"></t:errors>
典型用途**在調試 「result.hasErrors()」 行來自AppConfigController.java **
它看起來像我的JSR-303錯誤代碼(JSR-303註釋的「消息」屬性中的內容)不是由Spring進行翻譯/處理。相反,它被放置在「defaultMessage」屬性中。
org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'appConfig' on field 'applicationContactEmailAddress': rejected value []; codes [NotEmpty.appConfig.applicationContactEmailAddress,NotEmpty.applicationContactEmailAddress,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [appConfig.applicationContactEmailAddress,applicationContactEmailAddress]; arguments []; default message [applicationContactEmailAddress]]; default message [error.applicationContactEmailAddress.required]
Field error in object 'appConfig' on field 'homePageWarningTitleFr': rejected value []; codes [error.homePageWarningTitleFr.required.appConfig.homePageWarningTitleFr,error.homePageWarningTitleFr.required.homePageWarningTitleFr,error.homePageWarningTitleFr.required.java.lang.String,error.homePageWarningTitleFr.required]; arguments []; default message [null]
任何想法,我怎麼能告訴Spring到我的代碼添加到[]已按約定加入代碼([NotEmpty.appConfig.applicationContactEmailAddress,NotEmpty.applicationContactEmailAddress,NotEmpty.java.lang.String,NotEmpty]) ?
謝謝
對於該記錄,如果我使用其中一個預期的代碼(即:NotEmpty.appConfig.applicationContactEmailAddress)。我仍然想使用自定義消息,這樣我就可以利用我們的本地命名約定來處理i18n密鑰。謝謝。 –