spring boot 項(xiàng)目中使用thymeleaf模板的案例分析
準(zhǔn)備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;@Controllerpublic class PreregController {@ResourcePreregMapper 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;@Mapperpublic 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;}@Overridepublic 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>到達(dá)時(shí)間</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 項(xiàng)目中使用thymeleaf模板的案例分析的文章就介紹到這了,更多相關(guān)spring boot 使用thymeleaf模板內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁的方法3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. XHTML 1.0:標(biāo)記新的開端5. ASP基礎(chǔ)知識VBScript基本元素講解6. 利用CSS3新特性創(chuàng)建透明邊框三角7. XML入門的常見問題(四)8. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼9. 詳解CSS偽元素的妙用單標(biāo)簽之美10. HTML5 Canvas繪制圖形從入門到精通
