《穿越SpringBoot 》 第二章-SpringBoot Web开发 | 第1节- SpringBoot 整合 Thymeleaf

Spring Boot 整合Thymeleaf

前提:

借助:IntelliJ IDEAMaven构建工具,以及基于SpringBoot 2.3.4
官人如需使用 Maven 请阅读教程:Maven 构建工具的下载与安装
官人如需使用 IDEA 请阅读教程:IntelliJ IDEA

更多干货:

请阅读:《穿越SpringBoot》系列文章
请参考:Java学习资料

快速入门

下载官网文档:https://www.thymeleaf.org/ 快速入门哦!可以仿照下面的步骤自己操作。

在这里插入图片描述在这里插入图片描述

为什么选择Thymeleaf

我们知道Spring MVC本身是支持多种视图技术。视图技术不推荐使用 JSP,官方推荐使用一些第三方的模板引擎:ThymeleafFreeMarkerMustacheVelocity等各种模板引擎。
同时还为开发者提供了自定义模板扩展的支持。它可以完全替代 JSP 。使用嵌入式Servlet容器时,请避免使用JSP,
因为使用JSP打包后会存在一些限制,如想使用请参考下一节:《穿越SpringBoot 》 第二章-SpringBoot Web开发 | 第2节- SpringBoot 整合Jsp页面

特点:

动静结合:
Thymeleaf 在 有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

开箱即用:

它提供标准spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

多方言支持:

Thymeleaf 提供spring标准方言和一个与SpringMVC 完美集成的可选模块,可以快速的实现表单绑定属性编辑器国际化等功能。
与SpringBoot完美整合**
SpringBoot提供了Thymeleaf的默认配置,并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf。代码几乎没有任何区别,就是在模板语法上有区别。

本质上,它们跟Spring MVC继承时,是提供了Spring MVC的 ViewResolver 组件的自定义实现。

比如:Thymeleaf 的实现是 ThymeleafViewResolver 与解析JSP的InternalViewResolver类似.

使用

目录结构:

在这里插入图片描述

pom.xml依赖:

要想使用Thymeleaf 必须 在spring boot下的导入如下图所示依赖启动器。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>1012</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>1012</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<!--  thymeleaf启动器  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!--	web启动器	-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--	热部署	-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<!--	lombok	-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

编写DemoController类:

创建一个DemoController 用来映射HTTP请求与页面的跳转,用到的注解@Controller @RequestMapping
package com.example.boot1012.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Controller
public class DemoController {
    @RequestMapping("/demo1")
    public String demo1(Model model){
        Map<String,Object> gf1=new HashMap<>();
        gf1.put("name","苹果");
        gf1.put("price",15);
        Map<String,Object> gf2=new HashMap<>();
        gf2.put("name","香蕉");
        gf2.put("price",16);
        Map<String,Object> gf3=new HashMap<>();
        gf3.put("name","橘子");
        gf3.put("price",17);

        Map<String,Object> map=new HashMap<>();

        map.put("username","张三");
        map.put("age",30);
        map.put("fruit", Arrays.asList(gf1,gf2,gf3));
        model.addAttribute("user",map);
        return "demo1";
    }

}

编写demo1.html、fragment.html

演示:thymeleaf模板引擎相关用法:
具体怎么用请参考:Thymeleaf 模板引擎用法大全。
<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>demo1.html</title>
</head>
<link rel="stylesheet" href="../static/style.css" th:href="@{/style.css }">
<body>
    <!--header头部片段引用 th:insert="~{引用的文件名 ::fragment定义的头部名字}"-->
    <div th:insert="~{fragment::header}"/>
    <hr>
        <h1>Hello Thymeleaf</h1>
    <h2>Thymeleaf 模板</h2>
    <input type="text" name="username" value="李四" th:value="${user.username}">

    <br>
    
    <span th:if="${user.age <= 25}" th:text="${user.age}岁的 + '妹子'"></span>
    <span th:if="${user.age > 25}" th:text="|${user.age}岁的  老女人|"></span>
    
    <br>
    
    <ul>
        <li th:each="f : ${user.fruit}" th:text="${f.name}">水果</li>
    </ul>
    
    <ul>
        <li th:each="f : ${user.fruit}" th:object="${f}">
            <div>
                <span th:text="*{name}">水果</span>
                <span th:text="${f.price}">价格</span>
            </div>
        </li>
    </ul>

    <hr>

    <!--footer底部部片段引用 th:insert-->
    <div th:insert="~{fragment::footer}"/>
</body>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>fragment.html定义公用的片段</title>
</head>
<body>
    <div th:fragment="header">
        <h1>这是页面的header部分</h1>
    </div>
    <div th:fragment="footer">
        <h1>这是页面的footer部分</h1>
    </div>
</body>
</html>

application.properties 或yml配置文件

IntelliJ IDEA中使用thymeleaf模板的时候,发现每次修改静态页面都需要重启才生效
为了提高响应速度Thymeleaf关闭模板缓存,需要添加devtools(热部署)依赖可看上面的pom文件。
在application.properties配置文件关闭即可。
详细实现请参考穿越SpringBoot 》 第三章-SpringBoot 的配置 | 第4节- SpringBoot 中的Devtools热部署

在这里插入图片描述

测试:

启动后访问:`http://localhost:8080/demo1

在这里插入图片描述

快速了解一下启动器做了啥?

在这里插入图片描述
在这里插入图片描述

1. XxxProperties : 能找到对应的配置项的支持。
Thymeleaf也会根据前缀和后缀来确定模板文件的位置:
默认前缀:classpath:/templates/
默认后缀:.html

在这里插入图片描述

2. XxxAutoConfiguration : 就是自动配置的逻辑。**
知道它自动配置了什么,将来我可以直接使用。
当不满足我的需求时,如何替换默认配置。

在这里插入图片描述
在这里插入图片描述

总结:

待完善…
本教程基于最新的spring-boot-starter-parent:2.3.4RELEASE编写,目前很多大佬都写过关于SpringBoot的教程了,如有雷同,请多多包涵.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值