Springboot and dynamic resources
Springboot does not support jsp by default.
It is recommended to use the template engine (thymeleaf) for assembly:
Webpage=template+data
The template engine we use here is thymeleaf
Example:
Introduce thymeleaf dependency:
<!-- 引入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>
Simple introduction to thymeleaf
1. Where is the code written?
According to the source code, you only need to put the thymeleaf file in "classpath:/templates/" and the file suffix is ".html".
2. Example:
First define a controller:
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";
}
}
Create a new html page in the templates of the internal path
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p th:text = "${welcome}">thymeleaf</p>
</body>
</html>
test
to sum up:
th: replace the original html value
If ${welcome} is not obtained, thymeleaf is displayed
Otherwise, the obtained value is displayed.
Introduction to Thymeleaf
Official documents can be downloaded from the official website! https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.pdf
th: value problem (chapter ten)
th:xx
th:text get the text value, escape
th:utext gets the text value without escaping
Example:
<h1>hello</h1>
th:text shows the effect of rendering hello as h1
th:utext displays <h1>hello</h1> without rendering
Symbol problem (Chapter 4)
Traverse problem:
<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>
Unconsciously, the basics of springboot are almost finished! Hope to use it as soon as possible, hahaha, happy!