Springboot整合Thymeleaf引入公共的CSS和JS文件的方法

最近想搭建一个简单的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">
    <!-- Font Awesome -->
    <link rel="stylesheet" th:href="@{../../static/plugins/fontawesome-free/css/all.min.css}">
   <!-- Theme style -->
    <link rel="stylesheet" th:href="@{../../static/dist/css/adminlte.min.css}">
    <!-- 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})

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 可以很方便的集成 Thymeleaf 模板引擎,下面是整合步骤: 1.添加 Thymeleaf 依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2.配置 Thymeleaf 模板 在 `src/main/resources/templates/` 目录下创建一个 thymeleaf 模板文件,例如 index.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Thymeleaf Demo</title> </head> <body> <h1 th:text="${message}">Hello World!</h1> </body> </html> ``` 其中 `${message}` 表示从后台传递过来的数据。 3.配置视图解析器 在 application.properties 或 application.yml 文件中添加以下配置: ```yaml spring: thymeleaf: cache: false prefix: classpath:/templates/ suffix: .html encoding: utf-8 ``` 其中: - `cache` 表示是否开启缓存 - `prefix` 表示模板文件所在目录 - `suffix` 表示模板文件后缀 - `encoding` 表示模板文件编码 4.在 Controller 中使用 Thymeleaf 在 Controller 中设置需要传递到前端的数据,并指定要返回的模板文件名: ```java @Controller public class DemoController { @GetMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello, Thymeleaf!"); return "index"; } } ``` 其中: - `@Controller` 表示这是一个控制器 - `@GetMapping("/hello")` 表示处理 GET 请求,路径为 /hello - `Model` 用于存储需要传递到前端的数据 - `return "index"` 表示返回名为 index 的模板文件 5.运行项目 启动 Spring Boot 项目,访问 http://localhost:8080/hello 即可看到效果。 以上就是 Spring Boot 整合 Thymeleaf 的基本步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值