Spring Boot+Thymeleaf找不到邮件属性 [英] Spring Boot + Thymeleaf not finding message properties

查看:43
本文介绍了Spring Boot+Thymeleaf找不到邮件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring Boot和Thymeleaf创建一个Web应用程序,但在获取模板以使用属性文件中定义的消息时遇到了问题。它显示的不是属性文件中定义的消息,而是??form.welcome_en_GB??控制台没有记录任何错误。

项目结构如下

──┬ 🗁 src
  │   └─── 🗁 main
  │       ├─── 🗁 java
  │       │   └─── 🗁 com
  │       │       └─── 🗁 package
  │       │           ├─── 🗁 controller
  │       │           │   └─── FormController.java
  │       │           ├─── Application.java
  │       │           └─── ServletInitializer.java
  │       └─── 🗁 resources
  │           ├─── 🗁 static
  │           │   └─── home.html
  │           ├─── 🗁 templates
  │           │   ├─── form.html
  │           │   └─── form.properties
  │           └─── application.properties
  └─── pom.xml

Application.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

ServletInitializer.java

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

FormController.java

@Controller
@RequestMapping("/form")
public class FormController {
    private static final Logger log = LoggerFactory.getLogger(FormController.class);

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public ModelAndView getNewReportForm() {
        log.info("New form requested");
        ModelAndView mav = new ModelAndView("form");
        return mav;
    }
}

form.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link rel="stylesheet" type="text/css" media="all"/>
</head>
<body>
<p th:text="#{form.welcome}">Welcome!</p>
</body>
</html>

form.properties

form.welcome=Hello there!

推荐答案

我相信将form.properties更改为messages.properties并将其定位在资源文件夹的根目录中应该可以让Spring Boot自动选取它。

当我有多个消息文件时,我会在MessageSource bean中显式列出它们,以便MVC自动配置获取它们,例如:

@Bean
public MessageSource messageSource() {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:/some-mvc-messages", "classpath:/some-other-mvc-messages", "classpath:/another-projects/mvc-messages");
    messageSource.setUseCodeAsDefaultMessage(true);
    messageSource.setDefaultEncoding("UTF-8");
    messageSource.setCacheSeconds(5);
    return messageSource;
}

这篇关于Spring Boot+Thymeleaf找不到邮件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆