Spring Boot MessageSource在application.properties中不起作用

时间:2018-07-26 09:40:04

标签: java spring spring-mvc spring-boot internationalization

我正在将spring MVC转换为spring boot(2.0.3.RELEASE)项目。

我在Spring Boot中使用它,它有效:

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:/locale/normal/message", "classpath:/locale/validation/message");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

我尝试通过以下方式使用spring boot自动配置(通过application.properties)(删除上面的手动bean配置):

spring.messages.basename=classpath:/locale/normal/message,classpath:/locale/validation/message

我在下面尝试过,它也不起作用:

spring.messages.basename=/locale/normal/message,/locale/validation/message

我在下面阅读了他们的documentation

# INTERNATIONALIZATION (MessageSourceProperties)
spring.messages.always-use-message-format=false # Whether to always apply the MessageFormat rules, parsing even messages without arguments.
spring.messages.basename=messages # Comma-separated list of basenames (essentially a fully-qualified classpath location), each following the ResourceBundle convention with relaxed support for slash based locations.
spring.messages.cache-duration= # Loaded resource bundle files cache duration. When not set, bundles are cached forever. If a duration suffix is not specified, seconds will be used.
spring.messages.encoding=UTF-8 # Message bundles encoding.
spring.messages.fallback-to-system-locale=true # Whether to fall back to the system Locale if no files for a specific Locale have been found.
spring.messages.use-code-as-default-message=false # Whether to use the message code as the default message instead of throwing a "NoSuchMessageException". Recommended during development only.

我将消息属性文件放在类路径中:

src\main\resources\locale\normal
src\main\resources\locale\validation

我尝试了很多组合,但是没有用,为什么?

我遇到的错误:

javax.servlet.jsp.JspTagException: No message found under code 'login.timeout' for locale 'en'.

(我很确定消息代码在我的属性文件中的所有语言环境中。)

我正在使用Spring标签加载本地化消息:

<spring:message code="${param.error}" />

1 个答案:

答案 0 :(得分:0)

尝试使用#代替$-<spring:message code="#{param.error}" />