在application.properties文件中配置i18n的资源配置
###############I18n资源配置######################
spring.messages.basename=i18n/messages
spring.messages.cache-duration=3600
spring.messages.encoding=UTF-8
在messages.properties文件中添加一些配置
控制器的内容
@Controller
@RequestMapping("/th")
public class ThymeleafController {
@GetMapping("/index")
public String index(ModelMap map){
map.addAttribute("name","admin");
return "index";
}
}
由于使用的是Thymeleaf模板引擎,在templates目录中新建index.html文件,内容如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>测试页面</title>
</head>
<body>
你好,hello world
<span th:text="${name}"></span><br>
<div th:switch="${name}">
<p th:case="'huangbaokang'">huangbaokang</p>
<p th:case="#{setting.username}">admin</p>
<p th:case="*">其他</p>
</div>
</body>
</html>
其中加载i18n的语法使用#号
浏览器测试,我设置了访问context为/hbk
说明:这种实现可以根据业务具体需要,比如通用发送邮件账号等,判断当前用户是特殊用户,显示不同的内容,在这里只是具体学习了下thymeleaf的语法,及springboot的相关知识点。