spring boot 項目中使用thymeleaf模板的案例分析
準備
MySql數(shù)據(jù)庫,表Prereg,IDEA
數(shù)據(jù)庫中的表如下所示:
IDEA目錄結(jié)構(gòu)如下:
添加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>學生學號</td> <td>學生姓名</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>
效果圖如下:
到此這篇關(guān)于spring boot 項目中使用thymeleaf模板的案例分析的文章就介紹到這了,更多相關(guān)spring boot 使用thymeleaf模板內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring Boot thymeleaf模板引擎的使用詳解
- SpringBoot使用Thymeleaf模板引擎訪問靜態(tài)html的過程
- springBoot加入thymeleaf模板的方式
- Spring boot項目使用thymeleaf模板過程詳解
- SpringBoot使用thymeleaf模板過程解析
- Spring Boot集成Thymeleaf模板引擎的完整步驟
- SpringBoot中的Thymeleaf模板
- Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法
- Spring boot搭建web應用集成thymeleaf模板實現(xiàn)登陸
- 詳解spring Boot 集成 Thymeleaf模板引擎實例
- springboot用thymeleaf模板的paginate分頁完整代碼
- springboot中thymeleaf模板使用詳解
- Springboot Thymeleaf模板文件調(diào)用Java類靜態(tài)方法
- Java基礎(chǔ)總結(jié)之Thymeleaf詳解
相關(guān)文章
Struts2源碼分析之ParametersInterceptor攔截器
這篇文章主要介紹了Struts2源碼分析之ParametersInterceptor攔截器,ParametersInterceptor攔截器其主要功能是把ActionContext中的請求參數(shù)設置到ValueStack中,,需要的朋友可以參考下2019-06-06java 服務器接口快速開發(fā)之servlet詳細教程
Servlet(Server Applet)是Java Servlet的簡稱,稱為小服務程序或服務連接器,用Java編寫的服務器端程序,具有獨立于平臺和協(xié)議的特性,主要功能在于交互式地瀏覽和生成數(shù)據(jù),生成動態(tài)Web內(nèi)容2021-06-06Java實現(xiàn)優(yōu)雅的參數(shù)校驗方法詳解
這篇文章主要為大家詳細介紹了Java語言如何實現(xiàn)優(yōu)雅的參數(shù)校驗,文中的示例代碼講解詳細,對我們學習Java有一定是幫助,需要的可以參考一下2022-06-06注解@TableName,@TableField,pgsql的模式對應方式
這篇文章主要介紹了注解@TableName,@TableField,pgsql的模式對應方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04Spring?Security圖形驗證碼的實現(xiàn)代碼
本文介紹了如何在SpringSecurity自定義認證中添加圖形驗證碼,首先需要在maven中添加相關(guān)依賴并創(chuàng)建驗證碼對象,然后通過Spring的HttpSessionSessionStrategy對象將驗證碼存儲到Session中,感興趣的朋友跟隨小編一起看看吧2024-10-10