SpringBoot学习笔记六:ThymeLeaf模板引擎的引入

本文详细介绍了在SpringBoot项目中如何引入并使用ThymeLeaf模板引擎,包括添加依赖、配置模板路径,以及在HTML和Controller中的具体使用示例。

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

1.模板引擎作用

在这里插入图片描述

2.ThymeLeaf的使用:

  1. 在SpringBoot框架中引入ThymeLeaf依赖:
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 改修ThymeLeaf配置项
    查看自动配置org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties类的属性部分源码
@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    private Charset encoding;
    private boolean cache;
    private Integer templateResolverOrder;
    private String[] viewNames;
    private String[] excludedViewNames;
    private boolean enableSpringElCompiler;
    private boolean renderHiddenMarkersBeforeCheckboxes;
    private boolean enabled;
    private final ThymeleafProperties.Servlet servlet;
    private final ThymeleafProperties.Reactive reactive;
    }

可知默认模板位置为: “classpath:/templates/”
上述配置可以在Spring Boot的配置文件application.properties中修改,示例:

# 修改thymeleaf模板存放位置
spring.thymeleaf.prefix="classpath:/static/";
# 模板文件后缀名
spring.thymeleaf.suffix=".html"
  1. 在html中的使用语法示例

controller类代码:

package com.mjj.springinitializrdemo.controller;

import com.mjj.springinitializrdemo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(Model model, HttpSession session){
        Student stu = new Student("majunjie",17, "0");
        model.addAttribute("student",stu);
        return "student";    //不加@ResponseBody时,该返回值对应的是模板文件的文件名(不带后缀)
    }

    @RequestMapping("/list")
    public String getList(Model model){
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三",12,"0",new Date()));
        students.add(new Student("李四",13,"1",new Date()));
        students.add(new Student("王五",14,"1",new Date()));
        students.add(new Student("赵六",15,"0",new Date()));
        model.addAttribute("students",students);
        return "list";
    }
}

student.html代码:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">  <!--添加thymeleaf的命名空间-->
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<!-- thymeleaf内置对象的使用 比如#session  以及工具类对象的使用 比如#strings
  具体工具类和内置对象列表可查阅官网-->
<h1 th:text="${#strings.append('欢迎您,',#session.getId())}"></h1>
<h1 th:text="${'欢迎您,'+#session.getId()}"></h1>   <!--  两种字符串拼接方法-->
<hr/>

姓名:<p th:text="${student.stuNam}" >张三</p>  <!--属性的取值-->
年龄:<p >[[${student.age}]]</p>   <!--行内表达式-->
性别:<p th:if="${student.sex} == 1">男</p>   <!--条件表达式-->
<p th:if="${student.sex} == 0">女</p>

</body>
</html>

list.html代码:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<table>
    <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>生日</th>
    </tr>

<!--遍历语法-->
    <tr th:each="u : ${students}">
        <td>[[${u.stuNam}]]</td>
        <td>[[${u.age}]]</td>
        <td th:if="${#strings.equals(u.sex,'0')}">男</td>
        <td th:if="${#strings.equals(u.sex,'1')}">女</td>
        <td th:text="${#dates.format(u.birthday,'yyyy-MM-dd')}"></td>
    </tr>

</table>
</body>
</html>

效果截图:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值