這是泥瓦匠的第105篇原創html
文章工程:java
上一講,咱們用 MongoDB 來實現 WebFlux 對數據源的操做。那麼有了數據須要渲染到前臺給用戶展現。這就是本文關心的 View 層。View 的表現形式有不少,好比 JSON 和 HTML。開發中經常使用模板語言很常見的有 Thymeleaf、Freemarker等。那react
什麼是模板語言?git
常見的模板語言都包含如下幾個概念:數據(Data)、模板(Template)、模板引擎(Template Engine)和結果文檔(Result Documents)。github
數據是信息的表現形式和載體,能夠是符號、文字、數字、語音、圖像、視頻等。數據和信息是不可分離的,數據是信息的表達,信息是數據的內涵。數據自己沒有意義,數據只有對實體行爲產生影響時才成爲信息。web
模板,是一個藍圖,即一個與類型無關的類。編譯器在使用模板時,會根據模板實參對模板進行實例化,獲得一個與類型相關的類。算法
模板引擎(這裏特指用於Web開發的模板引擎)是爲了使用戶界面與業務數據(內容)分離而產生的,它能夠生成特定格式的文檔,用於網站的模板引擎就會生成一個標準的HTML文檔。spring
一種特定格式的文檔,好比用於網站的模板引擎就會生成一個標準的HTML文檔。瀏覽器
模板語言用途普遍,常見的用途以下:緩存
Spring Boot 推薦使用的模板語言是 Thymeleaf,那
官方的解釋以下:
Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
Thymeleaf 是現代的模板語言引擎,能夠獨立運行也能夠服務於 Web。主要目標是爲開發提供自然的模板,而且能在 HTML 裏面準確的顯示。
Thymeleaf 是新一代 Java 模板引擎,在 Spring 4 後推薦使用。目前是 Spring 5 天然更加推薦。
相似上面講的工程搭建,新建一個工程編寫此案例。工程如圖:
目錄以下
模板是會用到下面兩個目錄
本文重點在 Controller 層 和 templates 視圖的編寫。
在 pom.xml 配置新的依賴:
<dependencies> <!-- Spring Boot Web Flux 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- 模板引擎 Thymeleaf 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Spring Boot Test 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
這裏咱們增長了 Thymeleaf 依賴,但不用在 application.properties - 應用配置文件 配置人任何配置。默認啓動其默認配置,如需修改配置參考 Thymeleaf 依賴配置,以下:
spring.thymeleaf.cache=true # Enable template caching. spring.thymeleaf.check-template=true # Check that the template exists before rendering it. spring.thymeleaf.check-template-location=true # Check that the templates location exists. spring.thymeleaf.enabled=true # Enable Thymeleaf view resolution for Web frameworks. spring.thymeleaf.encoding=UTF-8 # Template files encoding. spring.thymeleaf.excluded-view-names= # Comma-separated list of view names that should be excluded from resolution. spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers. spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL. spring.thymeleaf.reactive.max-chunk-size= # Maximum size of data buffers used for writing to the response, in bytes. spring.thymeleaf.reactive.media-types= # Media types supported by the view technology. spring.thymeleaf.servlet.content-type=text/html # Content-Type value written to HTTP responses. spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL. spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
包括經常使用的 編碼、是否開啓緩存等等。
在 CityWebFluxController 控制層,添加兩個方法以下:
@GetMapping("/hello") public Mono<String> hello(final Model model) { model.addAttribute("name", "泥瓦匠"); model.addAttribute("city", "浙江溫嶺"); String path = "hello"; return Mono.create(monoSink -> monoSink.success(path)); } private static final String CITY_LIST_PATH_NAME = "cityList"; @GetMapping("/page/list") public String listPage(final Model model) { final Flux<City> cityFluxList = cityHandler.findAllCity(); model.addAttribute("cityList", cityFluxList); return CITY_LIST_PATH_NAME; }
解釋下語法:
而後編寫兩個視圖 hello 和 cityList,代碼分別以下:
hello.html:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"/> <title>歡迎頁面</title> </head> <body> <h1 >你好,歡迎來自<p th:text="${city}"></p>的<p th:text="${name}"></p></h1> </body> </html>
cityList.html:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"/> <title>城市列表</title> </head> <body> <div> <table> <legend> <strong>城市列表</strong> </legend> <thead> <tr> <th>城市編號</th> <th>省份編號</th> <th>名稱</th> <th>描述</th> </tr> </thead> <tbody> <tr th:each="city : ${cityList}"> <td th:text="${city.id}"></td> <td th:text="${city.provinceId}"></td> <td th:text="${city.cityName}"></td> <td th:text="${city.description}"></td> </tr> </tbody> </table> </div> </body> </html>
經常使用語法糖以下
還有不少使用參考官方方文檔 http://www.thymeleaf.org/docu...
下面運行工程驗證下。使用 IDEA 右側工具欄,點擊 Maven Project Tab ,點擊使用下 Maven 插件的 install
命令。或者使用命令行的形式,在工程根目錄下,執行 Maven 清理和安裝工程的指令:
cd springboot-webflux-4-thymeleaf mvn clean install
在控制檯中看到成功的輸出:
... 省略 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:30 min [INFO] Finished at: 2017-10-15T10:00:54+08:00 [INFO] Final Memory: 31M/174M [INFO] ------------------------------------------------------------------------
在 IDEA 中執行 Application
類啓動,任意正常模式或者 Debug 模式。能夠在控制檯看到成功運行的輸出:
... 省略 2018-04-10 08:43:39.932 INFO 2052 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:8080 2018-04-10 08:43:39.935 INFO 2052 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080 2018-04-10 08:43:39.960 INFO 2052 --- [ main] org.spring.springboot.Application : Started Application in 6.547 seconds (JVM running for 9.851)
打開瀏覽器,訪問 http://localhost:8080/city/hello ,能夠看到如圖的響應:
繼續訪問 http://localhost:8080/city/page/list , 發現沒有值,那麼按照上一講插入幾條數據便可有值,如圖:
這裏,探討了 Spring WebFlux 的如何整合 Thymeleaf 。整合其餘模板語言 Thymeleaf、Freemarker,就大同小異了。下面,咱們能會整合 Thymeleaf 和 MongoBD,實現一個總體的簡單案例。
本文示例讀者能夠經過查看下面倉庫的中的模塊工程名: 2-x-spring-boot-webflux-handling-errors:
若是您對這些感興趣,歡迎 star、follow、收藏、轉發給予支持!