我正在尝试在属性文件中定义验证错误消息。
但是我得到了Bean验证的默认消息。
该项目的构建工具是gradle,属性文件位于src / main / resources目录的根目录中。
18:01:15.614 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Size, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@73916461, min=0, message={javax.validation.constraints.Size.message}, max=64, payload=[Ljava.lang.Class;@7b17b3f4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.615 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence'
18:01:15.615 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
18:01:15.616 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence'
18:01:15.617 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value 1234567890 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Digits, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@445d756e, integer=10, message=事業所コードは10桁で入力してください, payload=[Ljava.lang.Class;@62b4b118, fraction=0}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.618 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.DigitsValidatorForNumber'
18:01:15.618 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
18:01:15.618 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.DigitsValidatorForNumber'
18:01:15.618 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value 120-0005 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Pattern, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={flags=[Ljavax.validation.constraints.Pattern$Flag;@33d5d213, groups=[Ljava.lang.Class;@1c8c2702, regexp=[0-9]{3}-[0-9]{4}, message=郵便番号は000-0000のように入力してください。, payload=[Ljava.lang.Class;@5b7779fa}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.618 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator'
18:01:15.619 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
18:01:15.619 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator'
18:01:15.620 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value テスト建物 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Size, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@73916461, min=0, message={javax.validation.constraints.Size.message}, max=32, payload=[Ljava.lang.Class;@7b17b3f4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.620 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence'
18:01:15.620 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence'
18:01:15.620 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value テストジギョウショ against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Size, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@73916461, min=0, message={javax.validation.constraints.Size.message}, max=64, payload=[Ljava.lang.Class;@7b17b3f4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.620 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.ConstraintValidatorManager - Constraint validator org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence@6ce5282c found in cache.
18:01:15.620 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value 03-0000-0002 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Size, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@73916461, min=0, message={javax.validation.constraints.Size.message}, max=15, payload=[Ljava.lang.Class;@7b17b3f4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.620 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence'
18:01:15.620 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence'
18:01:15.620 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value 03-0000-0001 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Size, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@73916461, min=0, message={javax.validation.constraints.Size.message}, max=15, payload=[Ljava.lang.Class;@7b17b3f4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.620 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.ConstraintValidatorManager - Constraint validator org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence@745ffd43 found in cache.
18:01:15.620 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value 葛飾区 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Size, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@73916461, min=0, message={javax.validation.constraints.Size.message}, max=12, payload=[Ljava.lang.Class;@7b17b3f4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.620 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence'
18:01:15.620 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence'
18:01:15.621 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value テストジギョウショ against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.NotBlank, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@2ab452e2, message={javax.validation.constraints.NotBlank.message}, payload=[Ljava.lang.Class;@5febe8e4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.621 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.NotBlankValidator'
18:01:15.621 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
18:01:15.621 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.NotBlankValidator'
18:01:15.621 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value 120-0005 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.NotBlank, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@2ab452e2, message={javax.validation.constraints.NotBlank.message}, payload=[Ljava.lang.Class;@5febe8e4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.621 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.ConstraintValidatorManager - Constraint validator org.hibernate.validator.internal.constraintvalidators.bv.NotBlankValidator@11b44c8a found in cache.
18:01:15.621 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.NotBlank, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@2ab452e2, message={javax.validation.constraints.NotBlank.message}, payload=[Ljava.lang.Class;@5febe8e4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.621 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.ConstraintValidatorManager - Constraint validator org.hibernate.validator.internal.constraintvalidators.bv.NotBlankValidator@11b44c8a found in cache.
18:01:15.625 [http-nio-8080-exec-2] TRACE o.h.v.r.PlatformResourceBundleLocator - ValidationMessages not found by thread context classloader
18:01:15.625 [http-nio-8080-exec-2] TRACE o.h.v.r.PlatformResourceBundleLocator - ValidationMessages not found by validator classloader
18:01:15.625 [http-nio-8080-exec-2] DEBUG o.h.v.r.PlatformResourceBundleLocator - ValidationMessages not found.
18:01:15.626 [http-nio-8080-exec-2] TRACE o.h.v.r.PlatformResourceBundleLocator - ContributorValidationMessages not found by thread context classloader
18:01:15.626 [http-nio-8080-exec-2] TRACE o.h.v.r.PlatformResourceBundleLocator - ContributorValidationMessages not found by validator classloader
18:01:15.626 [http-nio-8080-exec-2] DEBUG o.h.v.r.PlatformResourceBundleLocator - ContributorValidationMessages not found.
18:01:15.628 [http-nio-8080-exec-2] DEBUG o.h.v.r.PlatformResourceBundleLocator - org.hibernate.validator.ValidationMessages found.
18:01:15.634 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value 03-0000-0003 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Size, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@73916461, min=0, message={javax.validation.constraints.Size.message}, max=15, payload=[Ljava.lang.Class;@7b17b3f4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.634 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.ConstraintValidatorManager - Constraint validator org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence@745ffd43 found in cache.
18:01:15.634 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value 03-0000-0001 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.NotBlank, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@2ab452e2, message={javax.validation.constraints.NotBlank.message}, payload=[Ljava.lang.Class;@5febe8e4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.634 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.ConstraintValidatorManager - Constraint validator org.hibernate.validator.internal.constraintvalidators.bv.NotBlankValidator@11b44c8a found in cache.
18:01:15.634 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value 東京都 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Size, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@73916461, min=0, message={javax.validation.constraints.Size.message}, max=6, payload=[Ljava.lang.Class;@7b17b3f4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.634 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence'
18:01:15.634 [http-nio-8080-exec-2] TRACE o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence'
18:01:15.634 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.SimpleConstraintTree - Validating value テスト住所 against constraint defined by ConstraintDescriptorImpl{annotation=j.v.c.Size, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={groups=[Ljava.lang.Class;@73916461, min=0, message={javax.validation.constraints.Size.message}, max=32, payload=[Ljava.lang.Class;@7b17b3f4}, constraintType=GENERIC, valueUnwrapping=DEFAULT}.
18:01:15.634 [http-nio-8080-exec-2] TRACE o.h.v.i.e.c.ConstraintValidatorManager - Constraint validator org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForCharSequence@4322edf2 found in cache.
18:01:15.636 [http-nio-8080-exec-2] TRACE o.s.w.s.m.m.a.ServletInvocableHandlerMethod - Arguments: [ja, {office=Office(officeCode=1234567890, officeName=, officeKana=テストジギョウショ, zipCode=120-0005, pref=東京都, city=葛飾区, address=テスト住所, bldg=テスト建物, tel1=03-0000-0001, tel2=03-0000-0002, fax=03-0000-0003, loginId=null, password=null), org.springframework.validation.BindingResult.office=org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'office' on field 'officeName': rejected value []; codes [NotBlank.office.officeName,NotBlank.officeName,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [office.officeName,officeName]; arguments []; default message [officeName]]; default message [must not be blank]}, {}, Office(officeCode=1234567890, officeName=, officeKana=テストジギョウショ, zipCode=120-0005, pref=東京都, city=葛飾区, address=テスト住所, bldg=テスト建物, tel1=03-0000-0001, tel2=03-0000-0002, fax=03-0000-0003, loginId=null, password=null), org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'office' on field 'officeName': rejected value []; codes [NotBlank.office.officeName,NotBlank.officeName,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [office.officeName,officeName]; arguments []; default message [officeName]]; default message [must not be blank]]
18:01:15.637 [http-nio-8080-exec-2] INFO o.s.h.controller.OfficeController - CommonGeneralController:updateOffice:hasErrors
18:01:15.637 [http-nio-8080-exec-2] INFO o.s.h.controller.OfficeController - org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'office' on field 'officeName': rejected value []; codes [NotBlank.office.officeName,NotBlank.officeName,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [office.officeName,officeName]; arguments []; default message [officeName]]; default message [must not be blank]
package (my top level package name).config;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
@ComponentScan("(my top level package name)")
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("common/login");
registry.addViewController("/error").setViewName("common/error");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource =
new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:/application-messages");
messageSource.setCacheSeconds(-1);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
office.tel1=電話番号1
Max={0}は{1}以下でなければなりません。
Min={0}は{1}以上でなければなりません。
NotNull={0}が空白です。
NotBlank={0}が空白です。
Size ={0}は{2}以上{1}以下でなければなりません。
如果存在验证错误,Controller会将BindingResult对象传递给此方法。
public String getErrMsg(BindingResult bindingResult) {
StringBuilder alertMsg = new StringBuilder("<p>入力エラー</p>");
alertMsg.append("<ul>\n");
for (int i = 0; i < bindingResult.getErrorCount(); i++) {
alertMsg.append("<li>" + bindingResult.getAllErrors().get(i).getDefaultMessage() + "</li>\n");
if (i == bindingResult.getErrorCount() - 1) {
alertMsg.append("</ul>");
}
}
return alertMsg.toString();
}
然后Controller将返回值传递给Thymeleaf模板。 模板包含此HTML。
<span th:utext="${alertMsg}"></span>
答案 0 :(得分:0)
感谢Deinum先生的评论,我成功显示了预期的错误消息! 我更改了显示错误消息的方法。我如下更改了Thymeleaf模板。
<ul class="errMsg" th:if="${#fields.hasErrors('*')}">
<li th:each="err : ${#fields.errors('*')}" th:text="${err}">Input is incorrect</li>
</ul>