springboot学习笔记(十)
生活随笔
收集整理的這篇文章主要介紹了
springboot学习笔记(十)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
springboot與動態資源
springboot默認不支持jsp。
推薦使用模板引擎(thymeleaf)進行組裝:
?網頁=模板+數據
此處我們使用的模板引擎是thymeleaf
示例:
引入thymeleaf依賴:
<!-- 引入thymeleaf依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId></dependency><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-java8time</artifactId></dependency>thymeleaf簡單入門
1.代碼在哪里寫?
通過源碼得知,只需要將thymeleaf文件放入"classpath:/templates/",并且文件的后綴是".html"即可。
?2.示例:
首先定義一個控制器:
package com.example.demo.controller;import java.util.Map;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class BootController {@RequestMapping("welcome")public String welcome(Map<String,Object> map) {map.put("welcome", "welcome thymeleaf");return "result";} }?在內路徑的templates中新建一個html頁面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body><p th:text = "${welcome}">thymeleaf</p> </body> </html>測試
總結:
th: 替換原來html的值
如果沒有獲取到${welcome},則顯示thymeleaf
反之,顯示獲取到的值。
Thymeleaf使用介紹
官方文檔可以去官網上下載!https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.pdf
th:的取值問題(第十章)
th:xx
th:text? 獲取文本值,轉義
th:utext? 獲取文本值,不轉義
示例:
<h1>hello</h1>
th:text? 顯示將hello渲染為h1后的效果
th:utext? 顯示<h1>hello</h1>,不渲染
符號問題(第四章)
遍歷問題:
<table> <tr> <th>NAME</th> <th>PRICE</th> <th>IN STOCK</th> </tr> <tr th:each="prod : ${prods}"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td> </tr> </table>不知不覺,springboot基礎已經快學完了!希望能盡快的用到,哈哈哈,高興!
總結
以上是生活随笔為你收集整理的springboot学习笔记(十)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信这10个牛X的隐藏功能,你知道几个?
- 下一篇: 产品案例:这些呼声很高功能,微信为何还不