Spring Boot + Thymeleaf 未找到消息属性
Spring Boot + Thymeleaf not finding message properties
我正在尝试使用 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 引导自动拾取它。
当我有多个消息文件时,我明确地将它们列在 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;
}
我在使用 messages.properties 文件时遇到同样的问题
问题是 @EnableAutoConfiguration 没有添加到我的 Spring 启动文件
@SpringBootApplication
@EnableAutoConfiguration
public class Run {
public static void main(String[] args) {
SpringApplication.run(Run.class, args);
System.out.println("Run");
}
}
添加作品后
我正在尝试使用 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 引导自动拾取它。
当我有多个消息文件时,我明确地将它们列在 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;
}
我在使用 messages.properties 文件时遇到同样的问题 问题是 @EnableAutoConfiguration 没有添加到我的 Spring 启动文件
@SpringBootApplication
@EnableAutoConfiguration
public class Run {
public static void main(String[] args) {
SpringApplication.run(Run.class, args);
System.out.println("Run");
}
}
添加作品后