springboot学习笔记(十)
springboot與動(dòng)態(tài)資源
springboot默認(rèn)不支持jsp。
推薦使用模板引擎(thymeleaf)進(jìn)行組裝:
?網(wǎng)頁(yè)=模板+數(shù)據(jù)
此處我們使用的模板引擎是thymeleaf
示例:
引入thymeleaf依賴(lài):
<!-- 引入thymeleaf依賴(lài) --><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簡(jiǎn)單入門(mén)
1.代碼在哪里寫(xiě)?
通過(guò)源碼得知,只需要將thymeleaf文件放入"classpath:/templates/",并且文件的后綴是".html"即可。
?2.示例:
首先定義一個(gè)控制器:
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";} }?在內(nèi)路徑的templates中新建一個(gè)html頁(yè)面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body><p th:text = "${welcome}">thymeleaf</p> </body> </html>測(cè)試
總結(jié):
th: 替換原來(lái)html的值
如果沒(méi)有獲取到${welcome},則顯示thymeleaf
反之,顯示獲取到的值。
Thymeleaf使用介紹
官方文檔可以去官網(wǎng)上下載!https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.pdf
th:的取值問(wèn)題(第十章)
th:xx
th:text? 獲取文本值,轉(zhuǎn)義
th:utext? 獲取文本值,不轉(zhuǎn)義
示例:
<h1>hello</h1>
th:text? 顯示將hello渲染為h1后的效果
th:utext? 顯示<h1>hello</h1>,不渲染
符號(hào)問(wèn)題(第四章)
遍歷問(wèn)題:
<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>不知不覺(jué),springboot基礎(chǔ)已經(jīng)快學(xué)完了!希望能盡快的用到,哈哈哈,高興!
總結(jié)
以上是生活随笔為你收集整理的springboot学习笔记(十)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微信这10个牛X的隐藏功能,你知道几个?
- 下一篇: 产品案例:这些呼声很高功能,微信为何还不