如果你和我一样,是一名 Java 道路上的编程男孩,其实我不太建议你花时间学 Thymeleaf,当然他的思想还是值得借鉴的。但是他的本质在我看来就是 Jsp 技术的翻版(Jsp 现在用的真的很少很少)。弄前端完全可以直接上手前端框架 vue。

并竟学Java在我眼里,目前没有什么是不要学的。兼测试、运维、前端啥都要会点。另外就目前来说,学Java的人数恐怕仍然后端中最庞大的。

免费后台模板在文末,大家有需求可以直接下载。

我想如果不是学校作业,也不会心血来潮写这篇文章。

阅读本文收获

  1. 学会 Thymeleaf 常用语法♀️
  2. 知晓 Thymeleaf 如何与 SpringBoot 集成♀️
  3. 使用 Thymeleaf 完成学校老师作业
  4. 如果有需求,可以直接下个模板,结合SpringBoot 写个毕业设计

一、 Thymeleaf 初介绍

Thymeleaf 官网

Thymeleaf 官方文档

Thymeleaf是适用于 Web 和独立环境的现代服务器端 Java 模板引擎。

Thymeleaf 的主要目标是为您的开发工作流程带来优雅的自然模板——HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而加强开发团队的协作。

凭借 Spring Framework 的模块、与您最喜欢的工具的大量集成以及插入您自己的功能的能力,Thymeleaf 是现代 HTML5 JVM Web 开发的理想选择——尽管它还有更多功能。 ---官方介绍

二、SpringBoot 整合 Thymeleaf

主要针对我们在项目中最常见的几种用法进行讲解。同时我们也是在项目中直接讲 Thymeleaf 的用法。

2.1、新建 SpringBoot 项目

这个就不用说了哈,我想大家都是会这个的吧。

2.2、导入依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

2.3、SpringBoot 静态资源存放路径

首先我们将模板下载下来:

我们点进 doc ,将需要的页面文件复制到 resources/templates包下,将css、images、js复制到 resources/static包下。

2.4、书写配置文件

thymeleaf 可以配置的一些属性,这只是常见的哈。

spring:
thymeleaf:
enabled: true #开启thymeleaf视图解析
encoding: utf-8 #编码
prefix: classpath:/templates/ #前缀 当然默认也是这个,可以不配置
cache: false #是否使用缓存
mode: HTML #严格的HTML语法模式
suffix: .html #后缀名

2.5、编写Controller

我们以 登录页面 为例,写个Controller 跳转到 login.html。

@Controller
@RequestMapping
public class LoginController { /** * 跳转到登录页面*/
@GetMapping("/login")
public String login(){
return "login";
} /** * 模拟登录请求 */
@PostMapping("/doLogin")
public String doLogin(String username,String password){
if(username!=null&&password!=null){
System.out.println(username+password);
//重定向到 /indedx 请求 也可以重定向页面
return "redirect:/index";
}
return "login";
} /** * 跳转到index 页面*/
@GetMapping("/index")
public String index(){
return "index";
} }

2.6、启动项目&问题处理

启动类没啥要改的,直接跑。

启动项目后,访问 localhost:8080/login ,可能会出现一个 缺少css、js的页面。而不是下面这个成功的页面。

原因是在我们使用 Thyemleaf 后,在页面中就不应该再使用相对路径,如这种: <link rel="stylesheet" type="text/css" th:href="/css/main.css">

而是应该修改为:<link rel="stylesheet" type="text/css" th:href="@{/css/main.css}">

修改完之后,还应在 html 页面的头部做一下修改:

<html lang="en" xmlns:th="http://www.thymeleaf.org">

2.7、Thyemleaf 常用

Thymeleaf 官网快速入门介绍

Thymeleaf 官方文档

2.7.1、th:href | 链接 (URL) 表达式

其实我们刚刚已经说了这点:

//以前的
<link rel="stylesheet" type="text/css" href="/css/main.css">
//修改后的:
<link rel="stylesheet" type="text/css" th:href="@{/css/main.css}">

至于这么做的原因是由于Thymeleaf 的语法规则规定。

错误示例:

<link rel="stylesheet" type="text/css" th:href="@{/static/css/main.css}">

引入的资源路径千万不要静态资源路径的集合中路径的前缀。否则会导致请求不到资源。

我们在使用 Thymeleaf 的 @{} 修饰后,它会自己去 static 包下寻找。

注意:在springboot2.0版本以前拦截器会默认对静态资源不拦截,但是springboot 2.0 以后拦截器会拦截所有,所以需要重写addInterceptors方法,不管是自己的静态资源还是webjars中的资源,都要放行

当然我只是在这提上一嘴,本文没写拦截器相关知识。

2.7.2、th:text

我们都会有一个需要提示信息的时候,就是简单展示一段文本,如:

<p th:text="#{home.welcome}">Welcome to our grocery store!</p>

我们修改一下之前的Controller:

/*** 跳转到登录页面  */
@GetMapping("/login")
public String login(Model model){
model.addAttribute("systemName","学生管理系统");
return "login";
}

另外修改一下登录页面:

<div class="logo">
<h1 th:text="${systemName}"></h1>
</div>

2.7.3、th:action

表单提交我想是最常见的啦吧。

<form class="login-form" method="post" th:action="@{/doLogin}">
</form>

在这里提交的路径,也是需要用 @{} 包裹起来。

后端还是照写,没有变化。

2.7.4、th:each & th:if

循环、判断应该是没有哪里用不到的啦吧。

写个Student 类,稍后会用到

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Long id;
private String name;
private Integer age;
/**
* true 为男
* false 为女
*/
private Boolean gender;
}

写个controller

/**
* 添加多个学生
*/
@GetMapping("/doEach")
public String doEach(Model model){
List<Student> students = new ArrayList<>();
Student student1 = new Student(1L,"1号学生",20,true);
students.add(student1);
Student student2 = new Student(2L,"2号学生",21,true);
students.add(student2);
Student student3 = new Student(3L,"3号学生",22,false);
students.add(student3);
Student student4 = new Student(4L,"4号学生",23,true);
students.add(student4);
model.addAttribute("students",students);
return "each";
}

再写个 each.html 页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>for循环</title>
</head>
<body> <table border="1">
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
<tr th:each="student : ${students}" >
<td th:text="${student.id}"></td>
<td th:text="${student.name}"></td>
<td th:text="${student.age}"></td>
<!-- 三元表达式 -->
<td th:text="${student.gender}?男:女"></td>
<td th:if="${student.gender}">利用if判断性别 男</td>
<td th:if="${not student.gender}">利用if判断性别 女</td>
</tr>
</table>
</body>
</html>

成果:

2.8、小结

我只是简单的说了一下 Thymeleaf,它支持的东西还是有不少的,在这没有一一说明,有需求时,可直接查询 Thymeleaf 文档即可。

三、免费后台模板

1、免费的后台模板:Vail Admin

2、聚集多个免费的后台模板:免费模板

点进去直接下载就可以啦。在SpringBoot 项目中直接引用就可以啦。

四、自言自语

你好,我是博主宁在春

希望本篇文章能让你感到有所收获!!!

我们:待别日相见时,都已有所成

欢迎大家一起讨论问题,躺了

简书 | 宁在春

CSDN | 宁在春

掘金 | 宁在春

知乎 | 宁在春

博客园 | 宁在春

SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目的更多相关文章

  1. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  2. Springboot整合thymeleaf模板

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...

  3. 三、SpringBoot整合Thymeleaf视图

    目录 3.1 Thymeleaf视图介绍 3.2 创建SpringBoot项目 3.2 配置Thymeleaf 3.3 编写Demo 3.4 小结 3.1 Thymeleaf视图介绍 先看下官网的介绍 ...

  4. SpringBoot 整合thymeleaf

    1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...

  5. vue-cli webpack 快速搭建项目

    一.安装vue npm install vue -g 二.用vue-cli快速搭建项目 //全局安装vue-cli npm install install -g vue-cli //创建一个基于web ...

  6. (转载) android快速搭建项目积累

    android快速搭建项目积累 2016-04-05 20:07 519人阅读 评论(0) 收藏 举报  分类: android优化(8)   Rx技术(5)  版权声明:本文为博主原创文章,未经博主 ...

  7. 基于Vue-cli 快速搭建项目

    Vue-cli可以快速帮助我们创建一个项目,这是官方给我们提供的脚手架.下面我说一下vue-cli的使用方法. 一.准备工作 在使用vue-cli时,首先需要安装nodejs,npm,其次需全局安装v ...

  8. SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

    在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...

  9. (二)springboot整合thymeleaf模板

    在我们平时的开发中,用了很久的jsp作view显示层,但是标签库和JSP缺乏良好格式的一个副作用就是它很少能够与其产生的HTML类似.所以,在Web浏览器或HTML编辑器中查看未经渲染的JSP模板是非 ...

随机推荐

  1. Django使用tinymce富文本编辑器

    1 - 安装 pip install django-tinymce==2.6.0 2 - 注册app INSTALLED_APPS = ( ... 'tinymce', ) 3 - 在setting中 ...

  2. Mysql 面试宝典

    实时更新 你用过哪些数据库? mysql redis mysql 和 redis 的区别? 比较点 Mysql Redis 数据库类型 关系型 非关系型 作用 持久化层 存储需要持久化的数据,数据存在 ...

  3. XML基础——extensible markup language

    一.xml概念 1.xml和html区别 其中,xml是纯文本文件,跨语言:浏览器有html解析器也有xml解析器: 2.和properties配置文件区别 二.xml语法 1.基本语法 三.xml组 ...

  4. 用Java实现红黑树

    红黑树是众多"平衡的"搜索树模式中的一种,在最坏情况下,它相关操作的时间复杂度为O(log n). 1.红黑树的属性 红黑树是一种二分查找树,与普通的二分查找树不同的一点是,红黑树 ...

  5. C语言学习笔记---1.C语言概述

    1.典型C程序结构 2.C程序细节 2.1#include指令和头文件 #include这行代码是一条C预处理器指令(preprocessor directive).通常,C编译器在编译前会对源代码做 ...

  6. 计算字符串的长度.len,RuneCountInString

    内置函数len(),可以返回字符串/数组/切片/map/channel的长度. unicode/utf8包 函数:RuneCountInString(输入一个字符串),返回int类型的字符串长度.由于 ...

  7. Excel删除重复数据及用公式筛选重复项并标记颜色突出显示

    当表格记录比较多时,常常会有重复数据,而重复记录往往只希望保存一条,因此需要把多余的删除:在 Excel 中,删除重复数据有两种方法,一种是用"删除重复数据"删除,另一种是用&qu ...

  8. CodeForces - 706B Interesting drink(二分查找)

    Interesting drink Problem Vasiliy likes to rest after a hard work, so you may often meet him in some ...

  9. 【PHP数据结构】散列表查找

    上篇文章的查找是不是有意犹未尽的感觉呢?因为我们是真真正正地接触到了时间复杂度的优化.从线性查找的 O(n) 直接优化到了折半查找的 O(logN) ,绝对是一个质的飞跃.但是,我们的折半查找最核心的 ...

  10. Java基础系列(3)- HelloWorld详解

    HelloWorld 1.新建一个java文件 文件后缀名为.java Hello.java [注意点]系统可能没有显示文件后缀名,我们需要手动打开 2.编写代码 public class Hello ...