目的:
1、thymeleaf テンプレート
2、Freemarkerのテンプレート
thymeleaf テンプレート
利点thymeleaf:
ブラウザから直接、サーバーへのサーブレットの開発を展開することなく、標準のページをHTML5のサポートは、(ネットワークがどうなるか)を開くことができるようになります。
ケースのコード:
POMファイルには、対応に依存するJARパッケージを追加します
<依存>
<groupIdを> org.springframework.boot </ groupIdを>
<たartifactId>春・ブート・スタータthymeleaf </たartifactId>
</依存関係>
春ブーツの公式文書では、あなたがapplication.ymlファイルに次の行を追加し、開発中のバッファリングを防止することをお勧めします(キャッシュ正式な環境またはあなたが、偽近い、真のオープンを開きたいです)
春:
thymeleaf:
キャッシュ:偽
背景コード:
バックテストにエンティティクラスのユーザーを作成します。
パッケージcom.huangting.springboot01.entity。
輸入lombok.Data;
/ **
* @author 黄大娘
* @Company dogsun会社
* @Create 2019年11月8日夜07時01
* /
@データ
パブリック クラスユーザー{
プライベート文字列のUID。
プライベート文字列のuname。
パブリックユーザー(){
}
パブリックユーザ(文字列が文字列のuname、UID){
この .UID = UID。
この .uname = はuname。
}
}
フロントエンドのHTMLページに対応するバックグラウンド操作、thymeleafテンプレート
ThymeleafController
パッケージcom.huangting.springboot01.controller。
輸入com.huangting.springboot01.entity.User;
輸入org.springframework.stereotype.Controller;
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.servlet.ModelAndView。
輸入はjava.util.ArrayList;
輸入はjava.util.List;
/ **
* @author 黄色叔母
* @Company dogsun会社
* @Create 2019年11月8日午前11時17
* /
@コントローラ
@RequestMapping( "/ thymeleaf" )
パブリック クラスThymeleafController {
(@RequestMapping "/リスト" )
パブリック{()のModelAndViewリスト
ModelAndView andView = 新しいのModelAndView();
リスト一覧 = 新しいArrayListを();
list.add(新しいユーザー( "1"、 "大黄" ));
list.add(新しいユーザー( "2"、 "大娘" ));
list.add(新しいユーザー( "3"、 "大毛" ));
andView.addObject( "ユーザリスト" 、リスト)。
andView.addObject( "名前"、 "HT" )。
andView.addObject( "MSG"、 "<スパンのスタイル= '色:赤;'>これは、htmlタグ</ span>のである" );
// ジャンプページ
andView.setViewName( "リスト" );
リターンandView。
}
}
対応する背景の効果を示す、フロントページをHTML
するlist.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> <!--一定要注意引用这个网站-->
<head>
<meta charset="UTF-8">
<title>thymeleaf分析页面</title>
</head>
<body>
thymeleaf分析页面
<h2>显示文本</h2>
<span th:text="${name}"></span>
<h2>显示HTML</h2>
<div th:utext="${msg}"></div>
<h2>循环</h2>
<table>
<tr>
<td>用户id</td>
<td>用户名</td>
</tr>
<tr th:each="u:${userList}">
<td th:text="${u.uid}"></td>
<td th:text="${u.uname}"></td>
</tr>
</table>
<h2>包含页面</h2>
<div th:include="common/head2 :: html"></div>
</body>
</html>
head2.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>包含页面</title>
</head>
<body>
<div th:fragment="h1">
第一部分内容
《常记溪亭日暮,沉醉不知归路。》
</div>
<div th:fragment="h2">
第二部分内容
《昨夜雨疏风骤,浓睡不消残酒。》
</div>
<div th:fragment="h3">
第三部分内容
《莫许杯深琥珀浓。未成沈醉意先融。》
</div>
</body>
</html>
效果:
Freemarker模板