![](/img/trans.png)
[英]Spring 4 with thymeleaf Internationalization not identify message from resource properties file
[英]Thymeleaf set message resource without WEB
在這種情況下,沒有Web配置,沒有Spring MVC,沒有Spring Boot等。只是純應用程序,我想使用Thymeleaf傳輸靜態html資源,並以String形式獲取內容。
但是,在這種情況下,如何為百里香葉設置國際消息? 所以我可以在靜態html文件中使用像
<h1 th:text="#{messageA}">AAAAA</h1>
您只需要定義一個消息源。 例如,在我的spring配置中,我有一個main方法:
@Configuration
@ComponentScan(basePackages = {"spring.cli.config"})
public class Main {
public static void main(String... args) throws Exception {
ConfigurableApplicationContext spring = new SpringApplicationBuilder(Main.class)
.web(NONE)
.profiles("default")
.run(args);
TemplateEngine engine = (TemplateEngine) spring.getBean("engine");
Context context = new Context();
System.out.println(engine.process("template", context));
}
}
和一個配置文件(在@ComponentScan(basePackages = {"spring.cli.config"})
掃描@ComponentScan(basePackages = {"spring.cli.config"})
):
@Configuration
public class Thymeleaf {
@Bean
public MessageSource messageSource() throws Exception {
ReloadableResourceBundleMessageSource res = new ReloadableResourceBundleMessageSource();
res.setBasename("file:src/main/resources/messages");
return res;
}
}
我的項目src/main/resources/messages.properties
有一個文件。
# messages.properties
messageA=Hello I am message A
並在html中:
<h1 th:text="#{messageA}">AAAAA</h1>
解析為:
<div>Hello I am message A</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.