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

本篇文章来大家学习一下在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无法解析。

引入公共页面

通常页面中会有许多公共的部分,比如常见的头部(样式及导航)、底部(分页及版权)、左部(导航)等。这些相同的内容避免每个页面都重复编写,可使用引入来完成。

Thymeleaf提供了三种引入方式:

<body>
    <div th

猜你喜欢

转载自blog.csdn.net/wo541075754/article/details/103982854