原理
内联语法的格式为:[[${xxxx}]]
内联语法可以
- 读取服务器端变量
- 调用内置对象的方法等
示例
第一步
创建SpringBoot项目,添加依赖,编写application.yml,参看博客:SpringBoot 集成Thymeleaf
页面模板
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script th:inline="javascript">
/*<![CDATA[*/
let user = [[${dept}]];
let path = [[${#request.getContextPath()}]];
let cou = [[${#locale.getLanguage()+'_'+#locale.getCountry()}]];
console.info(user);
console.info(path);
console.info(cou)
/*]]>*/
</script>
</body>
</html>
## Controller
@RequestMapping("/inline")
public String inline(Model model) {
model.addAttribute("dept",new Dept(10,"sales","CHICAGO"));
model.addAttribute("data", Arrays.asList("aa","bb","cc","dd"));
return "inline";
}