springboot——thymeleaf返回带参数的页面

本文介绍如何在SpringBoot项目中使用Thymeleaf模板引擎,包括添加依赖、控制器方法设置及页面语法示例,展示如何传递参数并显示。

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

springboot thymeleaf返回带参数的页面

一、实现步骤

(1)依赖

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

(2)控制层

	@RequestMapping(value = "/test6", method = RequestMethod.GET)
	public String test(Model model) throws Exception {
		model.addAttribute("name", "hahahahah");
		model.addAttribute("currentDate", new Date());
		
		return "list";
	}

(3)页面

——springboot约定大于配置,资源文件默认存在resources/templates下

——前段页面需要满足thymeleaf的语法

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>test</title>
</head>
<style>
table td.td1{
	width:191px;
	text-align:left;
	padding-left:20px;
}
table td.td2{
	width:332px;
	text-align:left;
	padding-left:20px;
}
table tr{
	height:48px;
	font-family: PingFangSC-Medium;
	font-size: 14px;
	color: rgba(0,0,0,0.80);
	line-height: 48px;
}
table tr.topic{
	font-family: PingFangSC-Semibold;
	font-size: 20px;
	color: #FFFFFF;
	line-height: 20px;
}
table td.header{
	height:48px;
	font-family: PingFangSC-Medium;
	font-size: 14px;
	color: rgba(0,0,0,0.40);
	line-height: 48px;
	text-align:left;
	padding-left:20px;
}
table tr:nth-child(even) {
	background: #F7F7F7;
}
table th{
	text-align:left;
	padding-left:20px;
	font-family: PingFangSC-Medium;
	font-size: 14px;
	color: rgba(0,0,0,0.80);
}
</style>

<body>
<div align="center">
<span th:text="${currentDate}"></span>
<span th:text="${name}"></span>
</div>
</body>
</html>

(4)测试实现

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值