最近想搭建一个简单的web网站,以便以后接点私活,所以首先考虑单机模式下的框架搭建,分布式的框架相对前段搭建成本有点高,另外暂时对前端代码不是很熟悉,所以采用了springboot搭配thymeleaf模版的开发模式,开发过程中想把共通的css和js文件放在一个共通的base.html下,所以根据网上的说明,自己也研究了一阵子,代码如下,亲测好用。
html的文件目录如下:signin.html为我的登录页面,base.html为我的共通的文件

base.html代码如下:
title和links是以变量的形式传入的,因为每个引入base.html的页面的title和css需要自定义,所以留有变量
注意:application.yml下需要追加如下配置:
mvc:
static-path-pattern: /static/**
view:
prefix: classpath:/templates/
suffix: .html<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head th:fragment="common_header(title,links)">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- google font: source sans pro -->
<link rel="stylesheet"
href="https://fonts.googleapis.com/css?family=source+sans+pro:300,400,400i,700&display=fallback" rel="external nofollow" >
<!-- font awesome -->
<link rel="stylesheet" th:href="@{../../static/plugins/fontawesome-free/css/all.min.css}" rel="external nofollow" >
<!-- theme style -->
<link rel="stylesheet" th:href="@{../../static/dist/css/adminlte.min.css}" rel="external nofollow" >
<!-- jquery -->
<script type="text/javascript" th:src="@{../../static/plugins/jquery/jquery.min.js}"></script>
<!-- jquery ui 1.11.4 -->
<script type="text/javascript" th:src="@{../../static/plugins/jquery-ui/jquery-ui.min.js}"></script>
<!-- bootstrap 4 -->
<script type="text/javascript" th:src="@{../../static/plugins/bootstrap/js/bootstrap.bundle.min.js}"></script>
<!-- adminlte app -->
<script type="text/javascript" th:src="@{../../static/dist/js/adminlte.min.js}"></script>
<!-- datatables & plugins -->
<script type="text/javascript" th:src="@{../../static/plugins/datatables/jquery.datatables.min.js}"></script>
<script type="text/javascript" th:src="@{../../static/plugins/datatables-bs4/js/datatables.bootstrap4.min.js}"></script>
<script type="text/javascript" th:src="@{../../static/plugins/datatables-responsive/js/datatables.responsive.min.js}"></script>
<script type="text/javascript" th:src="@{../../static/plugins/datatables-responsive/js/responsive.bootstrap4.min.js}"></script>
<script type="text/javascript" th:src="@{../../static/plugins/datatables-buttons/js/datatables.buttons.min.js}"></script>
<script type="text/javascript" th:src="@{../../static/plugins/datatables-buttons/js/buttons.bootstrap4.min.js}"></script>
<script type="text/javascript" th:src="@{../../static/plugins/jszip/jszip.min.js}"></script>
<script type="text/javascript" th:src="@{../../static/plugins/pdfmake/pdfmake.min.js}"></script>
<script type="text/javascript" th:src="@{../../static/plugins/datatables-buttons/js/buttons.html5.min.js}"></script>
<script type="text/javascript" th:src="@{../../static/plugins/datatables-buttons/js/buttons.print.min.js}"></script>
<script type="text/javascript" th:src="@{../../static/plugins/datatables-buttons/js/buttons.colvis.min.js}"></script>
</head>
</html>signin.html的代码如:
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="en">
<!--需要添加此行标注为thymeleaf模板 -->
<head th:replace="common/base :: common_header(~{::title},~{})">
<title>sign in</title>
</head>
需要注意点:
1.是这里base是没有后缀html的
2.这里的title需要自定义,但是css文件不需要所以格式为:
common_header(~{::title},~{})如果css也需要自定义的话格式为:
common_header(~{::title},~{::link})附:不能引用最可能的原因
springboot项目的所有文件都必须要编译到target文件夹下才能运行,因此首先检查你的target目录下有没有静态资源。

如果这里根本就没有静态资源,自然肯定不能引用了。
是什么原因导致target/classes文件夹下没有静态资源的呢?
最可能的原因就是在项目的pom文件中,你没有指明要将.css、.js等这些文件编译进target文件夹中。必须要申明,这些文件才会被正确编译进去。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
这里申明了,resources目录下,**/*.*类型的文件将被编译进target/classes文件夹,也即是所有的文件,因此.css和.js类的文件就可以正确的编译进去。
如果改成如下这样:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>代表只将resources下的.properties文件、.xml文件、.yml文件编译进去target/classes文件夹,自然就没有静态资源了,就没法引用了。编译得到的target文件夹如下:

可以看到,按照如上pom文件,编译得到的target/classes文件夹下根本就没有静态资源,不可能引用成功。
总结
到此这篇关于springboot整合thymeleaf引入公共的css和js文件的方法及注意点的文章就介绍到这了,更多相关springboot引入公共css和js文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论