3 -【 SpringBoot 资源访问 】- 3 模板引擎 thymeleaf

1 模板引擎

JSP、Velocity、Freemarker、Thymeleaf

在这里插入图片描述
SpringBoot 推荐的 Thymeleaf,语法更简单,功能更强大。

  • 动静结合:Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
  • 开箱即用:它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
  • 多方言支持:Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
  • 与SpringBoot完美整合,SpringBoot提供了Thymeleaf的默认配置,并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf。代码几乎没有任何区别,就是在模板语法上有区别。

2 步骤一:在 pom.xml 中引入 thymeleaf

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3 关闭 thymeleaf 缓存

开发过程中建议关闭缓存

spring:
  thymeleaf:
    cache: false

4 编写 hello.html ,放到 resources/templates 下面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>
        Hello thymeleaf!
        <br>
        My name is Nancy!
    </h1>
</body>
</html>

5 编写 HelloController

package com.snow.snow_springboot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * 第一个Controller测试类
 *
 * @author: yang
 * @Date:
 * @Description:
 * @version v1.0
 */
@Controller
@RequestMapping("/templates")
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(){
        return "hello";
    }

}

浏览器请求 http://localhost:8080/templates/hello
会找到相应的 servlet,返回 hello,找到 resources/templates/hello.html,返回页面。

6 编写模板 hello.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>
        Hello thymeleaf!
        <br>
        My name is <span th:text="${name}"></span>!
    </h1>
</body>
</html>

导入了 thymeleaf 的名称空间

7 编写访问模板文件 HelloController

package com.snow.snow_springboot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 *
 * 第一个Controller测试类
 *
 * @author: yang
 * @Date:
 * @Description:
 * @version v1.0
 */
@Controller
@RequestMapping("/templates")
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(Map<String,Object> map){
        map.put("name", "Nancy");
        return "hello"; // 由于使用的是模板引擎,会找到相应的 servlet,返回 hello,找到 `resources/templates/hello.html`,返回页面。
    }

}

浏览器请求 http://localhost:8080/templates/hello
会找到相应的 servlet,返回 hello,找到 resources/templates/hello.html,返回页面。
测试:能成功渠道 yang。
在这里插入图片描述

补充一、thymeleaf 自动配置的原理

在这里插入图片描述
选中 thymeleaf
在这里插入图片描述
打开 ThymeleafAutoConfiguration

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

	public static final String DEFAULT_PREFIX = "classpath:/templates/";

	public static final String DEFAULT_SUFFIX = ".html";

    ......

只要我们把 HTML 页面放在 classpath:/templates/,thymeleaf 就能自动渲染。

补充二、thymeleaf 语法规则

th:text 改变当前元素里面的文本内容

th:任意html属性,来替换原生属性的值。

在这里插入图片描述

中文参考手册:链接:https://pan.baidu.com/s/1b00pEAoaWe5ydNAiN88QRw
提取码:njb6
复制这段内容后打开百度网盘手机App,操作更方便哦

注意

​ 在Idea中,我们需要在修改页面后按快捷键:Ctrl + Shift + F9 对项目进行rebuild才可以。

​ eclipse中没有测试过。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知所起 一往而深

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值