SpringBoot(四) Web開發 --- Thymeleaf、JSP
Spring Boot提供了spring-boot-starter-web為Web開發予以支援,spring-boot-starter-web為我們提供了嵌入的Tomcat以及Spring MVC的依賴。
模板引擎
Spring Boot支援多種模版引擎包括:
- FreeMarker
- Groovy
- Thymeleaf(官方推薦)
- Mustache
JSP技術Spring Boot官方是不推薦的,原因有三:
- tomcat只支援war的打包方式,不支援可執行的jar。
- Jetty 巢狀的容器不支援jsp
- Undertow
- 建立自定義error.jsp頁面不會覆蓋錯誤處理的預設檢視,而應該使用自定義錯誤頁面
Thymeleaf引擎模板
Thymeleaf是一款用於渲染XML/XHTML/HTML5內容的模板引擎。類似JSP,Velocity,FreeMaker等,它也可以輕易的與Spring MVC等Web框架進行整合作為Web應用的模板引擎。與其它模板引擎相比,Thymeleaf最大的特點是能夠直接在瀏覽器中開啟並正確顯示模板頁面,而不需要啟動整個Web應用。它的功能特性如下:
- Spring MVC中@Controller中的方法可以直接返回模板名稱,接下來Thymeleaf模板引擎會自動進行渲染
- 模板中的表示式支援Spring表示式語言(Spring EL)
- 表單支援,併兼容Spring MVC的資料繫結與驗證機制
- 國際化支援
Spring官方也推薦使用Thymeleaf,所以本篇程式碼整合就使用Thymeleaf來整合。
使用Thymeleaf
1.加入依賴
1 <dependency> 2<groupId>org.springframework.boot</groupId> 3<artifactId>spring-boot-starter-thymeleaf</artifactId>2.1.6</dependency> 4 5 <properties> 6<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> 7<!-- 佈局功能的支援程式thymeleaf3主程式layout2以上版本 --> 8<!-- thymeleaf2layout1--> 9<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version> 10</properties>
只要我們把HTML頁面放在classpath:/templates/,thymeleaf就能自動渲染;
2.匯入thymeleaf的名稱空間
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3.使用thymeleaf語法
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>成功!</h1> <!--th:text 將div裡面的文字內容設定為 --> <div th:text="${hello}">這是顯示歡迎資訊</div> </body> </html>
注:通過xmlns:th=” ofollow,noindex" target="_blank">http://www.thymeleaf.org “ 命令空間,將靜態頁面轉換為動態的檢視,需要進行動態處理的元素將使用“th:”字首。
Thymeleaf的預設引數配置和表示式
# THYMELEAF (ThymeleafAutoConfiguration) #開啟模板快取(預設值:true) spring.thymeleaf.cache=true #Check that the template exists before rendering it. spring.thymeleaf.check-template=true #檢查模板位置是否正確(預設值:true) spring.thymeleaf.check-template-location=true #Content-Type的值(預設值:text/html) spring.thymeleaf.content-type=text/html #開啟MVC Thymeleaf檢視解析(預設值:true) spring.thymeleaf.enabled=true #模板編碼 spring.thymeleaf.encoding=UTF-8 #要被排除在解析之外的檢視名稱列表,用逗號分隔 spring.thymeleaf.excluded-view-names= #要運用於模板之上的模板模式。另見StandardTemplate-ModeHandlers(預設值:HTML5) spring.thymeleaf.mode=HTML5 #在構建URL時新增到檢視名稱前的字首(預設值:classpath:/templates/) spring.thymeleaf.prefix=classpath:/templates/ #在構建URL時新增到檢視名稱後的字尾(預設值:.html) spring.thymeleaf.suffix=.html #Thymeleaf模板解析器在解析器鏈中的順序。預設情況下,它排第一位。順序從1開始,只有在定義了額外的TemplateResolver Bean時才需要設定這個屬性。 spring.thymeleaf.template-resolver-order= #可解析的檢視名稱列表,用逗號分隔 spring.thymeleaf.view-names=
Simple expressions:(表示式語法) a. Variable Expressions: ${...}:獲取變數值;OGNL; b. Selection Variable Expressions: *{...}:選擇表示式:和${}在功能上是一樣; 補充:配合 th:object="${session.user}: <div th:object="${session.user}"> <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p> <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p> <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p> </div> c. Message Expressions: #{...}:獲取國際化內容 d. Link URL Expressions: @{...}:定義URL; @{/order/process(execId=${execId},execType='FAST')} f. Fragment Expressions: ~{...}:片段引用表示式 <div th:insert="~{commons :: main}">...</div>
SpringBoot -- JSP引擎模板
在專案下新建一個webapp目錄,webapp這個用來存放jsp的目錄,靜態資源還是放在resources的static下面。
1.加入依賴
<!--WEB支援--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jsp頁面使用jstl標籤--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--用於編譯jsp--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
2.application.properties配置
spring.mvc.view.prefix: /WEB-INF/jsp/ spring.mvc.view.suffix: .jsp #配置程式埠,預設為8080 server.port= 8080 #使用者繪畫session過期時間,以秒為單位 server.session.timeout= # 配置預設訪問路徑,預設為/ server.context-path= # 配置Tomcat編碼,預設為UTF-8 server.tomcat.uri-encoding=UTF-8 # 配置最大執行緒數 server.tomcat.max-threads=1000
3.新增pom.xml配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>