1

I'm trying to define the validation error messages in a property file.
but I got the default message of Bean Validation.
The build tool of this project is gradle and property files are in the root of src/main/resources directory.

What I've Tried

  • Make Sure My Locale is "ja"
  • Change "NotBlank=" to "NotBlank.javax.validation.constraints="

Console Logs Around the Field Error Message

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]

Source Codes And Contents of Property Files

WebMvcConfig.java Implements WebMvcConfigurer

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;
  }
}

A Part of application-messages_ja.properties

office.tel1=電話番号1
Max={0}は{1}以下でなければなりません。
Min={0}は{1}以上でなければなりません。
NotNull={0}が空白です。
NotBlank={0}が空白です。
Size ={0}は{2}以上{1}以下でなければなりません。

How I Display The Validation Error Messages in My Front-end

If there are validation errors, Controller delivers the BindingResult object to this method.

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();
  }

And Controller deliver the return value to a Thymeleaf template. template contains this HTML.

<span th:utext="${alertMsg}"></span>
hallen0328
  • 31
  • 5
  • How are you performing your test? I think that the problem could probably be in the `MessageSource` creation. I would suggest to validate this by manually checking if it works right after creating it, by calling the [getMessages()](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/AbstractMessageSource.html#getMessage-java.lang.String-java.lang.Object:A-java.util.Locale-) method. If you know that the problem is there you can focus your efforts there. Also, for the locale code try `ja_JP` instead of just `ja`. – Master_ex Jul 01 '20 at 06:42
  • Also, I think that the resources from `classpath` are cached forever, so `messageSource.setCacheSeconds(5);` will not be honored. – Master_ex Jul 01 '20 at 06:44
  • @Master_ex I checked the Locale object that delivered to a Controller class. the languageTag field of the Locale object is "ja". As for the MessageSource, what should I set the cache second?? – hallen0328 Jul 01 '20 at 07:02
  • @Master_ex I've tried what you said but I got the same field error message. I changed the name of the property file(application-messsages_ja -> application-messsages_ja_JP) and changed cache seconds of the MessageSource instance to "-1". – hallen0328 Jul 01 '20 at 07:15
  • 1
    @hellen0328: Regarding the cache I am just saying that it will probably be ignored whatever you set it. It will be equivalent to setting it to `-1`. Also, you have a typo in the file name, you have 3 s in message. Initially I thought it was a typo in the question but check it. – Master_ex Jul 01 '20 at 07:16
  • @Master_ex Sorry, I've made typos in sending the comment. the actual file name was "application-messages_ja_JP". Also, I understand about the cache seconds, thank you. – hallen0328 Jul 01 '20 at 07:23
  • OK, based on the above I think it is clear that the `MessageSource` for some reason is not correctly configured. It seems that it has to do with the configuration of the basename. I don't have a specific suggestion. Maybe the file is not at the root of the classpath? You have a good starting point to investigate further. Check also [this](https://stackoverflow.com/questions/11150869/resourcebundle-not-found-for-messagesource-when-placed-inside-a-folder). If you don't solve it maybe uploading your package hierarchy will help others to help you. Good luck. – Master_ex Jul 01 '20 at 07:32
  • 1
    You are trying/configuring too many things. Remove the validator (configured by default and attached to the `MessageSource`) also remove the `RequestMappingHandlerMapping` which is configured by default as well. Make sure you also have `@EnableWebMvc` on your configuration (could you add your full `WebMvcConfig`?). – M. Deinum Jul 01 '20 at 07:39
  • @M.Deinum Thank you for your comment! I've changed ```WebMvcConfig``` as you said but I got the same field error message. (I edit this question and add my full `WebMvcConfig'. Please check this.) Also, I succeeded in displaying the message with thymeleaf (HTML is ```

    ```)
    – hallen0328 Jul 01 '20 at 07:50
  • How are you triggering the validation manually or using the validation on the controllers? What you can do, to get more information, is enable debug or trace logging to see what is happening with the conversion. And how are you displaying them in your front-end? – M. Deinum Jul 01 '20 at 08:39
  • @M.Deinum I triggered the validation with the controller. I saw the trace logs relating to the validation but I have no idea what causes the problem... I edited this question and add the trace logs relating the validation. Could you check this?? – hallen0328 Jul 01 '20 at 09:21
  • 1
    The problem is your own code. You shouldn't be doing that. Thymeleaf with forms has ways of rendering the message. You are bypassing all that. – M. Deinum Jul 01 '20 at 11:40
  • @M.Deinum Thank you for your great help! I tried to display the messages as below. ```
    • Input is incorrect
    ``` And I succeeded in getting the expected messages!
    – hallen0328 Jul 01 '20 at 14:44

1 Answers1

0

Thanks for M. Deinum's comment, I succeeded in display the expected error message! I changed the method of displaying the error message. I changed Thymeleaf template as below.

<ul class="errMsg" th:if="${#fields.hasErrors('*')}">
  <li th:each="err : ${#fields.errors('*')}" th:text="${err}">Input is incorrect</li>
</ul>
hallen0328
  • 31
  • 5