我是在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. 转自自己的关于落谷计数器【p1239】的题解

    本蒟蒻写这道题用了两天半里大概五六个小时.(我太弱了) 然后这篇题解将写写我经历的沟沟坎坎,详细的分析一下, 但是由于它很长,因此一定还有多余的地方,比如说我的 预处理,可能比较多余.但是我觉得,信息 ...

  2. 准确率、精确率、召回率、F-Measure、ROC、AUC

    先理解一下正类(Positive)和负类(Negetive),比如现在要预测用户是否点击了某个广告链接,点击了才是我们要的结果,这时,点击了则表示为正类,没点击则表示为负类. TP(True Posi ...

  3. vue中子组件直接修改父组件prop属性bug

    在有些时候,子组件直接修改父组件传来的 prop 对象的属性会出现不同步的问题. 比如,父组件传过来的一个对象 checkBoxObj: checkBoxObj:{ checked: false } ...

  4. C#mvc重新定向并在路径中使用html扩展名实现伪静态

    首先修改配置文件,增加下面的两个配置: 接下来,修改MapRoute为路由增加.html后缀 完成后,我们来验证一下刚才的成果: http://localhost:2279/Home/.html 一个 ...

  5. C#8.0中新特性之一:结构readonly成员

    结构struct成员支持readonly,用来限制被其修饰的成员不会改变结构的内部状态.加上7.2版本添加的readonly struct和ref readonly方法返回以及之前的字段声明修饰作用, ...

  6. 开源框架 openFrameworks

    转自:https://www.cnblogs.com/lidabo/p/9134174.html 此处仅供学习,版权属原作者: 作为一个图形图像方向的研究生,我经常都在和 OpenGL .OpenCV ...

  7. asp.net core react 项目实战(一)

    asp.net-core-react asp.net core react 简介 开发依赖环境 .NET Core SDK (reflecting any global.json): Version: ...

  8. spring boot 2 + shiro 实现权限管理

    Shiro是一个功能强大且易于使用的Java安全框架,主要功能有身份验证.授权.加密和会话管理.看了网上一些文章,下面2篇文章写得不错.Springboot2.0 集成shiro权限管理 Spring ...

  9. DRF单表序列化和反序列化

    安装 djangorestframework pip install djangorestframework 将rest_framework注册到app上 INSTALLED_APPS = [ 're ...

  10. 初探爬虫 ——《python 3 网络爬虫开发实践》读书笔记

    零.背景 之前在 node.js 下写过一些爬虫,去做自己的私人网站和工具,但一直没有稍微深入的了解,借着此次公司的新项目,体系的学习下. 本文内容主要侧重介绍爬虫的概念.玩法.策略.不同工具的列举和 ...