springboot的templates用法

本文介绍了SpringBoot中如何使用templates进行视图渲染。在Controller中添加视图处理,配置相关依赖后,SpringBoot能自动识别templates文件夹下的HTML文件。在HTML中需要注意转义和非转义的引用方式,文章展示了运行效果。

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

**

springboot的templates用法

**

@Controller
public class HelloController {
    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("msg","<h1>templates测试</h1>");
        model.addAttribute("users", Arrays.asList("lishao","liyuan"));
        return "/test";
    }
}

在controller中添加视图
在html中调用

<body>
<h3>test</h3>
<!--不转义-->
<div th:text="${msg}"></div>
<!--转义h1-->
<div th:utext="${msg}"></div>
<hr>
<h3 th:each="user : ${users}" th:text="${user}"></h3>

</body>

记得要导入templates的依赖
当你导入了templates依赖,
在这里插入图片描述
就会直接识别出来文件下的test,简单方便

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

html中也要导入

<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">

一个是转义一个是不转义
以下是运行的结果
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值