溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在spring boot 項目中使用thymeleaf模板

發(fā)布時間:2020-11-04 16:05:47 來源:億速云 閱讀:197 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了如何在spring boot 項目中使用thymeleaf模板,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

準(zhǔn)備
MySql數(shù)據(jù)庫,表Prereg,IDEA
數(shù)據(jù)庫中的表如下所示:

如何在spring boot 項目中使用thymeleaf模板

IDEA目錄結(jié)構(gòu)如下:

如何在spring boot 項目中使用thymeleaf模板

添加thymeleaf依賴:

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

開始添加代碼:
在controller包添加類“PreregController”

package com.example.demo.controller;

import com.example.demo.mapper.PreregMapper;
import com.example.demo.pojo.Prereg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class PreregController {
	@Resource
	PreregMapper preregMapper;
	
	@RequestMapping("/listPrereg")
	public String listPrereg(Model model)
	{
		List<Prereg> preregs=preregMapper.findAll();
		model.addAttribute("preregs",preregs);
		return "listPrereg";
	}
}

在Mapper包下添加映射interface:“PreregMapper”

package com.example.demo.mapper;

import com.example.demo.pojo.Prereg;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import java.util.List;

@Mapper
public interface PreregMapper {
@Select("SELECT * FROM Prereg")
List<Prereg> findAll();
}

在pojo包下添加類Prereg:

package com.example.demo.pojo;

import java.util.Date;

public class Prereg {
private String StuId;
private String StuName;
private String Trans;
private int IsCompany;
private int PeopleCount;
private Date ArrTime;

public String getStuId() {
return StuId;
}

public void setStuId(String stuId) {
StuId = stuId;
}

public String getStuName() {
return StuName;
}

public void setStuName(String stuName) {
StuName = stuName;
}

public String getTrans() {
return Trans;
}

public void setTrans(String trans) {
Trans = trans;
}

public int getIsCompany() {
return IsCompany;
}

public void setIsCompany(int isCompany) {
IsCompany = isCompany;
}

public int getPeopleCount() {
return PeopleCount;
}

public void setPeopleCount(int peopleCount) {
PeopleCount = peopleCount;
}

public Date getArrTime() {
return ArrTime;
}

public void setArrTime(Date arrTime) {
ArrTime = arrTime;
}

@Override
public String toString() {
return "Prereg{" +
"StuId='" + StuId + '\'' +
", StuName='" + StuName + '\'' +
", Trans='" + Trans + '\'' +
", IsCompany=" + IsCompany +
", PeopleCount=" + PeopleCount +
", ArrTime=" + ArrTime +
'}';
}
}

注:小技巧:定義好變量后,Alt+insert彈出“Generate”,選擇“Getter and Setter”,再選擇toString()即可完成。
最后是寫HTML頁面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>springboot-thymeleaf demo</title>
</head>
	<body>
		<table border="1" width="1000">
			<thead>
				<tr>
					<td>學(xué)生學(xué)號</td>
					<td>學(xué)生姓名</td>
					<td>到達時間</td>
					<td>家人陪伴</td>
					<td>陪伴數(shù)量</td>
					<td>交通工具</td>
				</tr>
			</thead>
			<tr th:each="item: ${preregs}">
				<td th:text="${item.stuId}"></td>
				<td th:text="${item.stuName}"></td>
				<td th:text="${item.arrTime}"></td>
				<td th:text="${item.isCompany}"></td>
				<td th:text="${item.peopleCount}"></td>
				<td th:text="${item.trans}"></td>
			</tr>
		</table>
	</body>
</html>

效果圖如下:

如何在spring boot 項目中使用thymeleaf模板

上述內(nèi)容就是如何在spring boot 項目中使用thymeleaf模板,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI