テンプレートエンジン
テンプレートおよびデータ統合、データ分析は、指定された場所に充填し、そして最終的にコンテンツ所望生成される
ような:JSP、速度、FreeMarkerの、Thymeleafおよび他
Thymeleaf SpringBootは、高度なテンプレートエンジンが推奨される
導入。
<!--引入Thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
プロパティに配置されthymeleafのバージョン番号を変更するには
、レイアウトは、上記2を必要とする
構文とThymeleafを使用して
<!-- Thymeleaf 3 -->
<org.thymeleaf-version>3.0.11.RELEASE</org.thymeleaf-version>
<org.thymeleaf.extras.springsecurity4-version>3.0.2.RELEASE</org.thymeleaf.extras.springsecurity4-version>
<nz.net.ultraq.thymeleaflayout-version>2.3.0</nz.net.ultraq.thymeleaflayout-version>
<thymeleaf-extras-java8time-version>3.0.1.RELEASE</thymeleaf-extras-java8time-version>
基本となるソースコードを見て、ビューのソースコードにいくつかのパーサ、ビューリゾルバなど、ThymeleafPropertiesのデフォルト設定では、Ctrlキーを押しポイント移動を追加しました
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
//只要我们把HTML页面放在classpath:/templates路径下,Thymeleaf就会自动渲染了
private String mode = "HTML";
private Charset encoding;
Thymeleaf公式文書https://www.thymeleaf.org/documentation.html
使用Thymeleaf
- 名前空間は、Thymeleafを導入しました
<html lang="en" xmlns:th="http://www.thymeleaf.org">
使用Thymeleaf構文
- 目:任意のHTMLプロパティ:プロパティ値元のHTMLを交換します
<div th:text="${hello}">欢迎信息</div>
将div中的text设置为hello请求
コントローラ:
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
@RequestMapping("/success")
public String success(Map<String,Object> map){
map.put("hello","你好");
return "success";
}
結果:
文法はThymeleafルール
-
第タグ構文
https://blog.csdn.net/fulq1234/article/details/85003273 -
式の構文
-
変数式:$ {...}
- オブジェクトのプロパティとメソッドを取得します
- 組み込みの基本的なオブジェクトを使用します
- ビルトインツール使用してオブジェクト
のテストには:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
<div th:text="${hello}">欢迎信息</div>
<hr/>
<!-- th:each每次遍历都会生成这个标签-->
<h2 th:text="${user}" th:each="user:${users}"></h2>
<hr/>
<h4>
<!--注意行内写法:[[]]和[()]其中[[]]就是相当于th:text;[()]相当于th:utext-->
<span th:each="user:${users}"> [[${user}]] </span>
</h4>
</body>
</html>
複数のエラー、デバッグ、および最終的には最後のコメントは、識別子によって引き起こされる可能性がエラーになりますた
デバッグの結果: