在使用springboot的過程中,如果使用thymeleaf作為模板文件,則要求HTML格式必須為嚴格的html5格式,必須有結束標簽,否則會報錯!
但是如果使用嚴格的Html5標簽編寫頁面那么就會很麻煩,編程效率低,所以需要像一般的Html一樣編寫的話,
解決辦法如下:
1、在application.yml中增加spring: thymeleaf: mode: LEGACYHTML5
參數配置,開啟使用非嚴格的HTML
spring:
application:
name: myshop-service-user-consumer
thymeleaf:
cache: false # 開發時關閉緩存,不然沒法看到實時頁面
mode: LEGACYHTML5 # 用非嚴格的 HTML
encoding: UTF-8
servlet:
content-type: text/html
server:
port: 8601
但是如果配置了spring: thymeleaf: mode
那么就需要額外引用net.sourceforge.nekohtml:nekohtml
依賴,否則會報錯;
<!-- 在使用thymeleaf是必須使用以下兩個依賴,否則無法找到templates下的html視圖對象 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>