JavaWeb学习之路——SpringBoot 中thymeleaf模板用法(三)
生活随笔
收集整理的這篇文章主要介紹了
JavaWeb学习之路——SpringBoot 中thymeleaf模板用法(三)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
thymeleaf模板用法
thymeleaf通過它特定的語法,對(duì)HTML的標(biāo)記做渲染,能夠訪問后臺(tái)的動(dòng)態(tài)數(shù)據(jù),實(shí)現(xiàn)靜態(tài)html界面的動(dòng)態(tài)化
1.添加架包
<!--引入動(dòng)態(tài)模板--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>2.創(chuàng)建目錄
創(chuàng)建src/main/resource/templates包:該目錄安全,該目錄下內(nèi)容不能夠通過外界直接訪問
1)controller里面加上屬性
用@Controller方式返回視圖,默認(rèn)會(huì)綁定/resource/templates里面的html
@RequestMapping("/test")public String index(User user, Model model) {model.addAttribute("user",user);System.out.println(user.toString());return "index";}2)html?格式,th:text?顯示值
<!DOCTYPE html><html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"><head><meta charset="UTF-8"><title>Test Thymeleaf</title></head><body><span th:text="hello"></span><span th:text="${user.name}"></span><span th:text="${user.password}"></span></body></html>3)結(jié)果展示:
?
視圖返回結(jié)果:
?
視圖返回結(jié)果:
?
總結(jié)
以上是生活随笔為你收集整理的JavaWeb学习之路——SpringBoot 中thymeleaf模板用法(三)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 后台开发人员面试内容——Redis非关系
- 下一篇: JavaWeb学习之路——SpringB