我是在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>

控制台报错如下:

-- ::18.333 ERROR  --- [nio--exec-] org.thymeleaf.TemplateEngine             :
  [THYMELEAF][http-nio--exec-] Exception processing template "success": Exception parsing document: template="success", line - column
-- ::18.335 ERROR --- [nio--exec-] 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 - column ] with root cause org.xml.sax.SAXParseException: 元素类型 "meta" 必须由匹配的结束标记 "</meta>" 终止。
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:) ~[na:1.8.0_161]
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:) ~[na:1.8.0_161]
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:) ~[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以上之后,就不会因为缺少结束标签而报错了。

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

  1. Exception processing template "success": Exception parsing document: template="success",

    代码很简单 package com.kele.controller; import org.springframework.stereotype.Controller;import org.sprin ...

  2. freemarker.template.TemplateException:Error parsing including template

    1.错误描述 freemarker.template.TemplateException:Error parsing including template ftl/main.ftl:on line 6 ...

  3. (报错解决)Exception encountered during context initialization

    转: (报错解决)Exception encountered during context initialization 关键词 JavaEE JavaWeb eclipse XML AspectJ ...

  4. Hibernate报错解决Error parsing JNDI name [],Need to specify class name

    能实现写数据,但是报错. 出错信息: 十月 21, 2016 3:46:18 下午 org.hibernate.Version logVersionINFO: HHH000412: Hibernate ...

  5. Archive required for library “xxx” cannot be read or is not a valid zip file报错解决

    在项目中导入别人的maven项目时报错:Archive required for library “xxx” cannot be read or is not a valid zip file 网上查 ...

  6. Python使用suds调用webservice报错解决方法:AttributeError: 'Document' object has no attribute 'set'

    使用python的suds包调用webservice服务接口,报错:AttributeError: 'Document' object has no attribute 'set' 调用服务接口代码: ...

  7. django报错解决:Invalid HTTP_HOST header: 'xxx.com'. You may need to add u'xxx.com' to ALLOWED_HOSTS.

    django版本:1.11.15 使用uwsgi+nginx运行django程序,出现报错,报错为:Invalid HTTP_HOST header: 'xxx.com:82'. You may ne ...

  8. String MVC @RequestParam(required=false) int XXX 参数为空报错解决方法

    今天在用@RequestParam(required=false) int XXX 取参数的时候,当参数没有的时候Spring默认赋值为空.而此时使用基本类型int,所以报错,建议使用包装类 Inte ...

  9. TypeError: document.body is null_js报错解决办法

    今天在使用如下js代码的时候,发现报错:document.body is null <script type="text/javascript"> var dw=doc ...

随机推荐

  1. Cesium 限制相机进入地下

    有时我们在Cesium操作时,点击鼠标中间滚轮可更改视角,有时会使相机进入地下,导致体验很差,网上说了很多中方法,效果都不好或者没效果,下面是我翻了源码找到的方法,亲测有效.如有问题可按照专栏上的联系 ...

  2. javaWeb实现验证码--代码超简单

    1.前端显示 HTML: <h3>验证码:</h3> <input type="text" name="validationCode&quo ...

  3. Yii2中多表关联查询

    准备条件: 1.首先准备两张表: customer(用户表)(id, name) order(订单表)(id, customer_id, price) customer 表和 order 表之间是一对 ...

  4. 痞子衡嵌入式:恩智浦i.MX RTxxx系列MCU开发那些事 - 索引

    大家好,我是痞子衡,是正经搞技术的痞子.本系列痞子衡给大家介绍的是恩智浦i.MX RTxxx系列微控制器相关知识. 恩智浦半导体于2018年10月发布的i.MX RTxxx系列开启了ML/AI MCU ...

  5. python3 print() 函数带颜色输出 示例

    1.1 实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示是27,用八进制表示就 ...

  6. mysql-5.7.27安装

    1,下载5.7.27安装包   百度网盘   (注:5.7.27要安装net faframework4.5.2) 2,创建my.ini及data文件 下载5.7.27后解压,在创建my.ini文件及d ...

  7. ES6-map数据结构,增加、删除、查找 方法(set get has delete clear ) 属性:size

    map数据结构: 本质上是键值对的集合,类似集合: 可以遍历,方法很多,可以跟各种数据格式转换. let json = { name:'ananiah', age:'18' } //效率低 需要遍历j ...

  8. 百度地图API 拖拽或点击地图位置获取坐标

    function setPlace(map,myValue,callback){ function getAddress(){ var pp = local.getResults().getPoi(0 ...

  9. library: Vulnhub Walkthrough

    网络主机探测: 端口主机扫描: ╰─ nmap -p1-65535 -sV -A -O -sT 10.10.202.136 21/tcp open ftp vsftpd 3.0.380/tcp ope ...

  10. 网站如何免费升级到HTTPS?

    最近在做网站SSL升级,看似简单的操作还是会遇到各种问题,现在和大家分享一下. 证书申请: 公司是创业公司,为了省成本准备申请免费证书,对比了一些证书商,最后选择使用沃通wosign提供的证书服务,发 ...