thymeleaf使用mode: LEGACYHTML5非嚴格模式Html需要添加nekohtml依賴否則報錯


在使用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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM