当前位置: 移动技术网 > IT编程>开发语言>Java > thymeleaf Exception processing template "xxx": Exception parsing document: template="xxx", line 6 - column 3报错解决的几种方法

thymeleaf Exception processing template "xxx": Exception parsing document: template="xxx", line 6 - column 3报错解决的几种方法

2019年12月18日  | 移动技术网IT编程  | 我要评论

我是在springboot项目使用thymeleaf作为模板引擎时报的错误

 

controller代码非常简单,如下所示:

    @requestmapping("/abc")
    public string hello(model model) {
        model.addattribute("msg","你好");
        return "success";
    }

 

前端success.html也很简单,代码如下:

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
<div th:text="${msg}">信息</div>
</body>
</html>

 

 

控制台报错如下:

2019-12-17 11:11:18.333 error 2300 --- [nio-8080-exec-3] org.thymeleaf.templateengine             : 
  [thymeleaf][http-nio-8080-exec-3] exception processing template "success": exception parsing document: template="success", line 6 - column 3
2019-12-17 11:11:18.335 error 2300 --- [nio-8080-exec-3] o.a.c.c.c.[.[.[/].[dispatcherservlet]    : 
  servlet.service() for servlet [dispatcherservlet] in context with path [] threw exception [request processing failed; 
  nested exception is org.thymeleaf.exceptions.templateinputexception:
     exception parsing document: template="success", line 6 - column 3] with root cause

org.xml.sax.saxparseexception: 元素类型 "meta" 必须由匹配的结束标记 "</meta>" 终止。
    at com.sun.org.apache.xerces.internal.util.errorhandlerwrapper.createsaxparseexception(errorhandlerwrapper.java:203) ~[na:1.8.0_161]
    at com.sun.org.apache.xerces.internal.util.errorhandlerwrapper.fatalerror(errorhandlerwrapper.java:177) ~[na:1.8.0_161]
    at com.sun.org.apache.xerces.internal.impl.xmlerrorreporter.reporterror(xmlerrorreporter.java:400) ~[na:1.8.0_161]

 

前端报错如图:

 

 

 通过console控制台的【exception parsing document: template="success", line 6 - column 3]】报错可以看出,thymeleaf在解析success.html的第六行时发生了错误。报错原因控制台也列出来了,即: 元素类型 "meta" 必须由匹配的结束标记 "</meta>" 终止

 

解决方法一

解决方法之一,就是我们将success.html文件的meta标签添加封闭符号,即加上斜杠即可,如下图:

 

结果:

 

 

 

解决办法二

在spring中使用thymeleaf的时候,会对html进行严格的语法校验,比如在本例中,页面html缺少了封闭符号/,就会报错而转到错误页。这实际并没有什么必要。因此,在第二种解决方法中,我们可以设置使得thymeleaf对html非严格检查。

1)在maven中添加依赖

        <!--引入nekohtml,配合spring.thymeleaf.mode=legacyhtml5使用,使thymeleaf 可以解析非严格html格式的文档-->
        <dependency>
            <groupid>net.sourceforge.nekohtml</groupid>
            <artifactid>nekohtml</artifactid>
            <version>1.9.22</version>
        </dependency>

 

 

2)在application.properties全局配置文件中加入如下配置

spring.thymeleaf.mode=legacyhtml5

 

 注:默认spring.thymeleaf.mode=html5,是严格检查 。legacyhtml5需要搭配nekohtml库才可用,实现thymeleaf非严格检查。

 

解决方法3

在我的例子中,默认使用的是2.1.6版本的thymeleaf

 

 

 

 通过thymeleaf3官方文档,得知可以用以下方法来修改thymeleaf版本,即在pom的properties标签加入如下配置

    <properties>
        <thymeleaf.version>3.0.9.release</thymeleaf.version>
        <!-- 布局功能的支持程序  thymeleaf3主程序  layout2以上版本 -->
        <!-- thymeleaf2   layout1-->
        <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
    </properties>

 

 

在更新thymeleaf版本到3以上之后,就不会因为缺少结束标签而报错了。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网