thymeleaf Exception processing template "xxx": Exception parsing document: template="xxx", line 6 - column 3报错解决的几种方法
我是在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报错解决的几种方法的更多相关文章
- Exception processing template "success": Exception parsing document: template="success",
代码很简单 package com.kele.controller; import org.springframework.stereotype.Controller;import org.sprin ...
- freemarker.template.TemplateException:Error parsing including template
1.错误描述 freemarker.template.TemplateException:Error parsing including template ftl/main.ftl:on line 6 ...
- (报错解决)Exception encountered during context initialization
转: (报错解决)Exception encountered during context initialization 关键词 JavaEE JavaWeb eclipse XML AspectJ ...
- Hibernate报错解决Error parsing JNDI name [],Need to specify class name
能实现写数据,但是报错. 出错信息: 十月 21, 2016 3:46:18 下午 org.hibernate.Version logVersionINFO: HHH000412: Hibernate ...
- 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 网上查 ...
- Python使用suds调用webservice报错解决方法:AttributeError: 'Document' object has no attribute 'set'
使用python的suds包调用webservice服务接口,报错:AttributeError: 'Document' object has no attribute 'set' 调用服务接口代码: ...
- 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 ...
- String MVC @RequestParam(required=false) int XXX 参数为空报错解决方法
今天在用@RequestParam(required=false) int XXX 取参数的时候,当参数没有的时候Spring默认赋值为空.而此时使用基本类型int,所以报错,建议使用包装类 Inte ...
- TypeError: document.body is null_js报错解决办法
今天在使用如下js代码的时候,发现报错:document.body is null <script type="text/javascript"> var dw=doc ...
随机推荐
- PHP中抽象类和接口的区别
抽象类 抽象类无法被实例化,它的作用是为所有继承自它的类定义(或部分实现)接口. 使用 abstract 关键字定义抽象类. 可以像在普通类中那样在抽象类中创建方法和属性,在大多数情况下,一个抽象类至 ...
- C#_.NetCore_WebAPI项目_EXCEL数据导出(ExcelHelper_第二版_优化逻辑)
项目需要引用NPOI的Nuget包:DotNetCore.NPOI-v1.2.2 本篇文章是对WebAPI项目使用NPOI操作Excel时的帮助类:ExcelHelper的改进优化做下记录: 备注:下 ...
- js实现弹出框跟随鼠标移动
又是新的一天网上冲浪,在bing的搜索页面下看到这样一个效果: 即弹出框随着鼠标的移动而移动.思路大概为: 调用onmousemove函数,将鼠标的当前位置赋予弹出框即可 //html <div ...
- C# Newtonsoft.Json JsonSerializerSettings配置
JsonSerializerSettings常用配置整理 1.忽略某些属性 MemberSerialization.OptIn 默认情况下,所有的成员不会被序列化,类中的成员只有标有特性JsonPro ...
- Spring Boot 2.X 国际化
国际化文件的编写 messages.properties init project 7月前 messages_en_US.properties init project 7月前 messages_zh ...
- UWP 使用FontIcon
通常在设置按钮内容的时候,我们一般会写上文字,比如 <Button Content="OK"/> 但是有一些特殊情况,比如我们的按钮需要一个图标, 这个时候就需要一些特 ...
- weed3-1.hello world
Weed3 一个微型ORM框架(只有0.1Mb哦) 源码:https://github.com/noear/weed3 源码:https://gitee.com/noear/weed3 05年的时候开 ...
- FineUICore基础版部署到docker实战
FineUI用了好多年,最近出了FineUICore版本,一直没时间是试一下docker,前几天买了一个腾讯云服务器,1核2g,装了centos7.6,开始的时候主要是整个个人博客,在腾讯云安装了宝塔 ...
- ribbon客户端负载均衡
Ribbon简介 参考:https://blog.csdn.net/chengqiuming/article/details/80711168 Ribbon是Netflix发布的负载均衡器,它有助于控 ...
- 天下代码一大抄,整个案例的搬是什么鬼!疑似冒充蚂蚁金服高级Java开发工程师?你大爷
写在开始 上班前的第一件事,就是码云看看有什么消息,回复下网友的问题.如果看到喜欢的项目会点进去瞅瞅,然后就开始一天的工作. 然而,这一天的工作并不开心,一个今日热门项目让自己很恼火,一开始感觉并没有 ...