现在把html页面中的需要引入的js和css公共部分提取成一个模板,让其他页面在引入这个模板时,只要传入一个当前html的title即可。
模板文件 commonHead.html(存放路径/resources/templates/common/commonHead.html)
<head th:fragment="head(title)">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title th:text="${title}">The awesome application</title>
<script type="text/javascript" th:src="@{/static/common/js/jquery/1.8.3/jquery-1.8.3.js}"></script>
<script type="text/javascript" th:src="@{/static/common/js/jquery-impromptu/4.2/jquery-impromptu.4.2.js}"></script>
<link rel="stylesheet" type="text/css" th:href="@{/static/common/css/table.css}"/>
<link rel="stylesheet" type="text/css" th:href="@{/static/common/js/jquery-impromptu/4.2/jquery-impromptu.css}"/>
</head>
需要引用模板的网页 test.html
<html>
<head th:include="common/commonHead.html :: head('测试页面')"></head>
<!--或者<head th:include="common/commonHead.html :: head(title='测试页面')"></head>-->
<body>
</body>
</html>
参考链接:
https://blog.csdn.net/whatlookingfor/article/details/78321451
https://github.com/thymeleaf/thymeleaf/issues/350
https://stackoverflow.com/questions/22093149/thymeleaf-fragments-and-default-parameters