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)測試實現