java - 如何在 thymeleaf 中包含 message.properties

标签 java spring spring-boot thymeleaf

我正在使用带有 thymeleaf 的 spring boot。这是我的项目结构: enter image description here

这是我的应用启动类:

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

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

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

我已经尝试了各种方法来解决这个配置问题。请问,有人可以帮忙吗?

最佳答案

引用spring boot官方文档

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

它说

Static resources can be moved to /public (or /static or /resources or /META-INF/resources) in the classpath root. Same for messages.properties (Spring Boot detects this automatically in the root of the classpath).

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

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

#messages
spring.messages.basename=locale/messages

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

关于java - 如何在 thymeleaf 中包含 message.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30106659/

相关文章:

java - 如何运行 amazon s3 中的 java jar 文件

java - 如何从 SWT 中的表中删除 CCombo

javascript - 有没有办法从java代码执行javascript函数

带有@PreAuthorize 注解的java.lang.NoSuchMethodError

java - 如何在log4j2中使用docker传递的环境变量?

java - Spring Boot 中的 Kafka 配置类找不到 keystore 或信任库

java - 制作带有可见边框的透明 JFrame

Spring RestTemplate - 需要释放连接?

java - Spring security 返回 401,将日期作为输入发送到其余 Controller

java - Spring项目中的org.springframework.beans.NotWritablePropertyException