Double Kill,springboot与thymeleaf整合!

1、引依赖的故事

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

从依赖上可以看出,有thymeleaf的启动器starter,就一定会有默认配置。

在这里插入图片描述
自动配置了thymeleaf模板的位置classpath:/templates/,及后缀名.html。

在这里插入图片描述

在这里插入图片描述

比着葫芦画个飘,根据springboot依赖,找其他技术的默认配置属性,就是这个样子。

2、创建thymeleaf文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 th:text="${'多喝热水:'+msg}">thymeleaf模板页</h1>
</body>
</html>

hello.html文件

可以看得出来,基本上就是html文件,并没有什么乱七八糟的新东西。

唯一的区别就是多了命名空间.

xmlns:th="http://www.thymeleaf.org"

3、后台代码

整体结构
在这里插入图片描述
application.yml文件内容

关闭缓存,因为Thymeleaf默认开启页面缓存,提高页面并发能力,但这样会导致我们修改页面不能立即展示效果,即热部署。

server:
  port: 8089
spring:
  thymeleaf:
    cache: false

ThymeleafStarter启动类

package com.laoben.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ThymeleafStarter {

    public static void main(String[] args) {
        SpringApplication.run(ThymeleafStarter.class,args);
    }
}


HelloController文件

package com.laoben.demo.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("msg", "Hello, Thymeleaf!");
        return "hello";
    }
}

4、测试

在这里插入图片描述

················································································
你学废了吗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值