JavaWeb学习之路——SpringBoot 中thymeleaf模板用法(三)
生活随笔
收集整理的這篇文章主要介紹了
JavaWeb学习之路——SpringBoot 中thymeleaf模板用法(三)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
thymeleaf模板用法
thymeleaf通過它特定的語法,對HTML的標記做渲染,能夠訪問后臺的動態數據,實現靜態html界面的動態化
1.添加架包
<!--引入動態模板--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>2.創建目錄
創建src/main/resource/templates包:該目錄安全,該目錄下內容不能夠通過外界直接訪問
1)controller里面加上屬性
用@Controller方式返回視圖,默認會綁定/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)結果展示:
?
視圖返回結果:
?
視圖返回結果:
?
總結
以上是生活随笔為你收集整理的JavaWeb学习之路——SpringBoot 中thymeleaf模板用法(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 后台开发人员面试内容——Redis非关系
- 下一篇: JavaWeb学习之路——SpringB