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 <!-- thymeleaf2 layout1--> 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=”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>