Sel*_*aly 4 spring properties-file thymeleaf spring-boot
我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)
Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
ServletInitializer.java
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
form.properties
form.welcome=Hello there!
Run Code Online (Sandbox Code Playgroud)
Dav*_*wer 11
我相信更改form.propertiesto 的名称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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9253 次 |
| 最近记录: |