Spring-boot Thymeleaf Html5 SAXParseException 解析錯誤

thymeleaf 解析 html5 出錯

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>SB Admin - Start Bootstrap Template</title>
  <!-- Bootstrap core CSS-->
  <link href="../static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <!-- Custom fonts for this template-->
  <link href="../static/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
  <!-- Page level plugin CSS-->
  <link href="../static/vendor/datatables/dataTables.bootstrap4.css" rel="stylesheet">
  <!-- Custom styles for this template-->
  <link href="../static/css/sb-admin.css" rel="stylesheet">
</head>

SAXParseException

org.xml.sax.SAXParseException: 元素類型 "link" 必須由配對的結束標記 "</link>" 終止。
org.xml.sax.SAXParseException: 元素類型 "meta" 必須由配對的結束標記 "</meta>" 終止。

Solution

application.yml 設定

spring:
  thymeleaf:
    mode: LEGACYHTML5

看了一下 spring-boot-starter-thymeleaf 裡面用的 thymeleaf 的版本是 2.1.6

官網文件 - http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html

XML: for XML that does not require validation during parsing.
VALIDXML: for XML that should be validated during parsing.
XHTML: for XHTML 1.0 or 1.1 templates that do not need validation.
VALIDXHTML: for XHTML 1.0 or 1.1 templates that should be validated during parsing.
HTML5: for HTML5 templates that are well-formed XML documents.
LEGACYHTML5: for HTML5 templates that are not well-formed XML documents, and therefore need a previous preprocessing step for tag balancing, syntax correction, etc.

所以改為 LEGACYHTML5, 但還需要搭配 nekohtml 套件。

pom.xml

<!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>

Result

參考

沒有留言:

張貼留言