Spring Boot + Thymeleaf没有找到消息属性

时间:2016-04-11 10:08:19

标签: spring spring-boot thymeleaf properties-file

我正在尝试使用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!

1 个答案:

答案 0 :(得分:0)

使用messages.properties文件时遇到同样的问题 问题是我的Spring启动文件中没有添加@EnableAutoConfiguration

 @SpringBootApplication
 @EnableAutoConfiguration
    public class Run {

        public static void main(String[] args) {
            SpringApplication.run(Run.class, args);
            System.out.println("Run");
        }

    }

添加工作后