ReloadableResourceBundleMessageSource无法正常工作

时间:2015-04-18 10:51:03

标签: spring hibernate spring-mvc spring-security thymeleaf

目前正在开发一个没有XML bean文件的新Spring 4 + Thymeleaf + Security项目。不知何故,本地化属性文件没有加载,我无法在日志文件中找到任何关于它们的内容。

/**
 * Generates the i18n language loader.
 *
 * @return
 */
@Bean(name="messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
    ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
    resource.setBasename("classpath:messages");
    resource.setDefaultEncoding("UTF-8");
    resource.setUseCodeAsDefaultMessage(true);
    resource.setFallbackToSystemLocale(false);
    return resource;
}

/**
 * Generates the i18n language changer parameter.
 *
 * @return
 */
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("language");
    return localeChangeInterceptor;
}

/**
 * Genereates the default i18n language.
 *
 * @return
 */
@Bean(name = "localeResolver")
public SessionLocaleResolver localeResolver() {
    SessionLocaleResolver localeResolver = new SessionLocaleResolver();
    localeResolver.setDefaultLocale(new Locale("en"));
    return localeResolver;
}

/**
 * Generates the template resolver for thymeleaf.
 *
 * @return
 */
@Bean
public ServletContextTemplateResolver templateResolver() {
    ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode("HTML5");
    resolver.setCacheable(false);
    return resolver;

}

/**
 * Generates the template engine for thymeleaf.
 *
 * @return
 */
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setTemplateResolver(templateResolver());
    engine.addDialect(new SpringSecurityDialect());
    return engine;
}

/**
 * Generates the view resolver from Spring MVC with thymeleaf intergrated.
 *
 * @return
 */
@Bean
public ViewResolver viewResolver() {
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    viewResolver.setOrder(1);
    viewResolver.setViewNames(new String[]{"*"});
    viewResolver.setCache(false);
    return viewResolver;
}

/**
 * Add resources.
 *
 * @param registry
 */
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/assets/**").addResourceLocations("/WEB-INF/assets/");
}

/**
 * Add interceptors.
 * 
 * @param registry 
 */
@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
}

src / main / resources /文件夹中的文件名:

  • messages.properties
  • messages_en.properties
  • messages_nl.properties

有没有人可以帮助我?

3 个答案:

答案 0 :(得分:1)

在ReloadableResourceBundleMessageSource的基本名称模式中缺少表示类路径根的斜杠字符“/”。

resource.setBasename("classpath:/messages");

答案 1 :(得分:0)

Javabase配置:

<input pattern="\d{1,5},\d{2}" required>

XmLbase配置:

注意:您应该已经添加了mvc名称空间。

@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {

       @Bean
       public MessageSource messageSource() {
             ReloadableResourceBundleMessageSource messageSource = new
                    ReloadableResourceBundleMessageSource();
             messageSource.setBasename("classpath:messages");
             messageSource.setCacheSeconds(10);
             return messageSource;
       }

       @Bean
       public LocalValidatorFactoryBean localValidatorFactoryBean() {
              LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
              factoryBean.setValidationMessageSource(messageSource());
              return factoryBean;
        }

        @Override
        public Validator getValidator() {
              return localValidatorFactoryBean();
        }
 }

注意:如果您尝试从&#34; webapp&#34;文件夹所以不需要使用&#34; classpath:&#34;字首。

答案 2 :(得分:0)

我需要为我的开发环境进行以下配置:

application.properties:

spring.messages.basename=file:src/main/resources/messages
spring.messages.cache-duration=0

Java 配置:

@Bean
@ConfigurationProperties(prefix = "spring.messages")
public MessageSourceProperties messageSourceProperties() {
    return new MessageSourceProperties();
}

@Bean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME)
public ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource( MessageSourceProperties properties ) {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    if (StringUtils.hasText( properties.getBasename() )) {
        messageSource.setBasenames( StringUtils.commaDelimitedListToStringArray( StringUtils.trimAllWhitespace( properties.getBasename() ) ) );
    }
    if (properties.getEncoding() != null) {
        messageSource.setDefaultEncoding( properties.getEncoding().name() );
    }
    messageSource.setFallbackToSystemLocale( properties.isFallbackToSystemLocale() );
    Duration cacheDuration = properties.getCacheDuration();
    if (cacheDuration != null) {
        messageSource.setCacheMillis( cacheDuration.toMillis() );
    }
    messageSource.setAlwaysUseMessageFormat( properties.isAlwaysUseMessageFormat() );
    messageSource.setUseCodeAsDefaultMessage( properties.isUseCodeAsDefaultMessage() );
    return messageSource;
}

说明

spring.messages.basename=file:src/main/resources/messages => 直接从源读取文件 spring.messages.cache-duration=0 => 禁用要重新加载的文件的缓存。默认为 -1,表示永久缓存。

ReloadableResourceBundleMessageSource bean 是 Spring 的 MessageSourceAutoConfiguration.java impl 的直接副本,但使用 ReloadableResourceBundleMessageSource 作为消息源。

Bean 需要具有名称 AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME 以禁用默认自动配置。