代码之家  ›  专栏  ›  技术社区  ›  sagar limbu

无法访问区域设置消息

  •  0
  • sagar limbu  · 技术社区  · 6 年前

    我在参考资料目录中定义了两个属性文件。 它们是:- 信息。属性

    message.welcome=Se registro correctamente. Le enviaremos
    

    留言。属性

    message.welcome=Welcome To our main login page
    

    现在,我正试图通过如下方式使用thymeleaf在我的html页面上访问此消息:-

    <h1 th:text="#{message.welcome}"></h1>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. 
      Obcaecati voluptatibus odio vero et quasi, incidunt quae eaque 
      maiores repellendus totam placeat autem quam eligendi ut in 
      veritatis. Dolores, repellendus dolor.</p>
    

    和我的配置文件:-

    @Configuration
    public class MvcConfig implements WebMvcConfigurer {
    
        @Autowired
        private MessageSource messageSource;
    
    
        public MvcConfig() {
            super();
        }
    
    
        @Bean
        public LocaleResolver localeResolver(){
            SessionLocaleResolver localeResolver = new SessionLocaleResolver();
            localeResolver.setDefaultLocale(Locale.US);
            return  localeResolver;
        }
    
    
        @Bean
        public LocaleChangeInterceptor localeChangeInterceptor(){
            LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
            lci.setParamName("lang");
            return lci;
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(localeChangeInterceptor());
        }
    
        @Override
        public Validator getValidator() {
            LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
            validator.setValidationMessageSource(messageSource);
            return validator;
        }
    
    }
    

    enter image description here enter image description here

    为什么没有显示欢迎信息?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Mạnh Quyết Nguyễn    6 年前

    从代码结构来看,它看起来像是移动了 messages 到子目录: /messages/messages_en.properties 因此违约 messageSource 我看不出来。

    您需要覆盖默认设置 消息源 :

    @Bean
    @Primary // Must use this to override message source
    public static MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:messages/messages");
        messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
        messageSource.setFallbackToSystemLocale(true);
        return messageSource;
     }
    

    把这个放到你的 MvcConfig