如何在thymeleaf中包含message.properties

osa*_*gie 3 java spring thymeleaf spring-boot

我正在使用带百里香的弹簧靴.这是我的项目结构: 在此输入图像描述

这是我的App开始课程:

    @EnableAutoConfiguration
@Configuration
@ComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在home.leaf.html上有这个: <p th:text = "#{username}"></p>

但是当我运行这个应用程序时,这就是我得到的:?? username_en_US ??

我已经尝试了各种方法来解决此配置问题.拜托,有人可以帮忙吗?

Far*_*ook 10

请参阅弹簧启动的官方文档

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-convert-an-existing-application-to-spring-boot

它说

静态资源可以移动到类路径根目录中/public(/static/resources/META-INF/resources).相同 messages.properties(Spring Boot会在类路径的根目录中自动检测到这一点).

因此,您应该创建ur国际化文件messages.properties并放在根类路径中.

或者,您也可以通过在application.properties文件中添加此条目来将默认位置编辑到更合适的位置

#messages
spring.messages.basename=locale/messages
Run Code Online (Sandbox Code Playgroud)

因此,您可以使用名称messages.properties或任何特定语言将文件存储在资源文件夹内的locale文件夹中.