zl程序教程

您现在的位置是:首页 >  其他

当前栏目

SpringBoot2.x系列教程(三十二)Thymeleaf资源导入及公共布局

资源导入教程 系列 布局 公共 thymeleaf SpringBoot2
2023-09-27 14:28:48 时间

本篇文章来大家学习一下在Thymeleaf下如何引入静态资源及通用代码块。

引入静态资源

Thymeleaf中引入静态资源比较简单,与前面讲到的@{…}语法一致。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Hello Thymeleaf</title>
    <link rel="stylesheet" type="text/css" th:href="@{css/style.css}"/>
    <!--<link rel="stylesheet" type="text/css" href="css/style.css"/>-->
    <script th:src="@{js/test.js}"></script>
    <!--<script src="js/test.js"></script>-->
</head>
<body>

    <button onclick="alertMsg()">Hello</button>

</body>
</html>

上述代码中通过th:href、th:src结合@{…}语法引入了静态资源内容。

注意:使用Thymeleaf的引用方法,只有运行项目才有效。普通打开HTML无法解析。