How to display messages in Thymeleaf and Spring Boot?

后端 未结 5 1857
深忆病人
深忆病人 2021-02-01 05:58

I created a Spring Boot web application that uses Thymeleaf as the template engine. I configured the MessageSource to look for messages in a subfolder:



        
相关标签:
5条回答
  • 2021-02-01 06:22

    Because I am using Spring Boot, the MessageSource is configured with a MessageSourceAutoConfiguration. These settings can be easily changed in the application.properties file. In my case I had to add the following to this file:

    spring.messages.basename=i18n/messages
    
    0 讨论(0)
  • 2021-02-01 06:22

    The way I resolved the i18n messaging was to define the MessagesSource bean like you. Additionally I had to override the getValidator() method of the WebMvcConfigurerAdapter.

    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource( messageSource() );
        return validator;
    }
    

    After this, it worked for me.

    0 讨论(0)
  • 2021-02-01 06:23

    Is there a file messages.properties (no _de) present to allow fallback? Does it work? Is your browser set to locale DE_de ?

    0 讨论(0)
  • 2021-02-01 06:31

    And add this to your application.properties file

    #messages
    spring.messages.basename=i18n/messages
    

    and store the file n the correct folder as specified above.

    you don't need messageSource bean

    0 讨论(0)
  • 2021-02-01 06:38

    The message source be relative to classpath:

    messageSource.setBasename("  classpath:i18n/messages");
    

    Here is a tutorial I referenced for thymeleaf and spring = http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html

    0 讨论(0)
提交回复
热议问题