SpringBoot的模板引擎和@Controller层的使用,一定要关闭模板引擎的缓存,否则修改HTMl文件不会生效

本文介绍了如何在SpringBoot中集成Thymeleaf模板引擎,包括POM配置、模板作用、HTML配置,以及如何关闭缓存。还涵盖了@Controller与@RestController的区别,并列举了常用配置和关键知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:springboot的模板引擎thymeleaf

1.thymeleaf在pom.xml配置

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

2.模板引擎的作用
模板引擎的作用就是我们来写一个页面模板,比如有些值呢,是动态的,我们写一些表达式。而这些值,从哪来呢,我们来组装一些数据,我们把这些数据找到。然后把这个模板和这个数据交给我们模板引擎,模板引擎按照我们这个数据帮你把这表达式解析、填充到我们指定的位置,然后把这个数据最终生成一个我们想要的内容给我们写出去,这就是我们这个模板引擎。
在themplates目录下的所有页面,只能通过controller来跳转。
这个只能通过thymeleaf跳转。
3.在html文件中配置

<html lang="en" xmlns:th="http://www.thymeleaf.org">
   Variable Expressions变量:${}
   Selection Variable Expressions选择表达式:*{}
   Message Expressions信息:#{}
   Link URL Expressionsurl:@{}
   Fragment Expressions片段表达:~{}

样例:
Controller层

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

@Controller
public class TestController {
    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("msg","hello ,Spriboot");
        return "test";
    }
}

templates

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${msg}"></div>

</body>
</html>

4.thymeleaf语法
在这里插入图片描述

二:关闭模板引擎

模板引擎,默认开启缓存
开发的时候,需要关闭模板引擎的缓存
在application.yaml文件中
添加,

spring:
  thymeleaf:
    cache: false

三:springboot中application.properties常用配置和必记知识

1.application.propertries

/#关闭默认图标
spring.mvc.favicon.enabled=false
#关闭模板引擎的缓存
spring.thymeleaf.cache=false

2.使用@Controller注解跳转页面必须增加thymeleaf配置

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

3.@RestController和@Controller的区别
(1).@RestController无法返回页面,@Controller可以
(2).@RestController可以直接返回数据,可以自己在方法李确定返回数据的类型@Controller必须借助@RequestBody

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@黑夜中的一盏明灯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值