Thymeleaf整合Springboot(idea)

本文介绍了如何在IDEA中新建SpringBoot项目并整合Thymeleaf,通过修改properties文件配置Thymeleaf的相关参数,如前缀、后缀、模板格式等,以实现HTML模板的正确解析。同时提到了Thymeleaf的缓存设置,并指出HTML文件应放在templates文件夹下,最后简要提及了controller层的关联。

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

由于想在以前搭建的项目使用(以前使用jsp),注释掉jsp相关之后,反正都不能行得通。还是新建整合吧,idea新建springboot可以自行百度。
首先引入Thymeleaf (创建的时候也可以勾选,我没有)

**

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

**

properties文件配置

spring.thymeleaf.prefix=classpath:/templates/
####后缀
spring.thymeleaf.suffix=.html
#####模板格式
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false


html文件默认放置在template文件夹

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
Thymeleaf模板引擎
<h1 th:text="${name}"></h1>

<table border="1">
    <tr>
        <th>序号</th>
        <th>用户id</th>
        <th>用户名</th>
        <th>描述</th>
        <th>图片</th>
        <th>创建时间</th>
        <th>状态变量:last</th>
        <th>状态变量:size</th>
        <th>状态变量:current</th>
    </tr>
    <tr  th:each="user,userStat:${list}">
        ###可以运算
        <th th:text="${userStat.index-5}"></th>
        <th th:text="${user.id}">状态变量:current</th>
        <th th:text="${user.name}">状态变量:name</th>
        <th th:text="${user.detail}">状态变量:odd</th>
        <th th:text="${user.pic}">状态变量:first</th>
        <th th:text="${user.createtime}">状态变量:last</th>
        <th th:text="${userStat.last}">状态变量:last</th>
        <th th:text="${userStat.size}">状态变量:size</th>
        <th th:text="${userStat.current}">状态变量:current</th>
    </tr>
</table>
</body>
</html>

controller层

@RequestMapping("/thymeleaf")
public String index(ModelMap map){
    map.addAttribute("name","Thymeleaf测试");
    List<ItemsVo> list =  mybatisService.selectAll();
    map.addAttribute("list",list);
    return "index";
}

效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值