Springbootと動的リソース
Springbootはデフォルトでjspをサポートしていません。
組み立てにはテンプレートエンジン(thymeleaf)を使用することをお勧めします。
Webページ=テンプレート+データ
ここで使用するテンプレートエンジンは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";
}
}
内部パスのテンプレートに新しい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:値の問題(第10章)
th:xx
th:textはテキスト値を取得し、エスケープします
th:utextは、エスケープせずにテキスト値を取得します
例:
<h1>こんにちは</ h1>
th:textは、helloをh1としてレンダリングする効果を示しています
th:utextはレンダリングせずに<h1> hello </ h1>を表示します
シンボルの問題(第4章)
トラバースの問題:
<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>
無意識のうちに、スプリングブーツの基本はほぼ完成です!できるだけ早くそれを使用したいと思っています、ハハハ、幸せです!