异常处理-SpringBoot中thymeleaf对应前台页面大于号\小于号使用问题

在SpringBoot使用Thymeleaf时遇到内部服务器错误500,原因是解析文档时在index模板第58行第28列发生错误。错误源于HTML元素内容格式不正确。为解决此问题,需要在HTML页面添加<!DOCTYPE html>声明,并在application.properties中关闭模板缓存以实现界面文件的实时更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

浏览器报错信息:
There was an unexpected error (type=Internal Server Error, status=500).
Exception parsing document: template="index", line 58 - column 28


后台报错信息:
ERROR 72788 --- [nio-8086-exec-1] o.thymeleaf.templateparser.ErrorHandler  : [THYMELEAF][http-nio-8086-exec-1] Fatal error during parsing
org.xml.sax.SAXParseException: 元素内容必须由格式正确的字符数据或标记组成。
ERROR 72788 --- [nio-8086-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8086-exec-1] Exception processing template "index": Exception parsing document: template="index", line 58 - column 28
ERROR 72788 --- [nio-8086-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/energymanagement] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template="index", line 58 - column 28] with root cause

程序代码信息:定位到Html页面中的代码,如下图

问题详解及处理方式:

Springboot支持thymeleaf、freemarker、JSP,但是官方不建议使用JSP,因为有些功能会受限制。

1、在pom.xml中添加依赖,指定前台模版:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、开发的时候要关闭模板缓存,不然修改界面文件后无法实时显示。在application.properties文件中关闭模板缓存:

spring.thymeleaf.cache = false

3、编写前台界面:

注意:在编写html页面的时候,如果不加 <!DOCTYPE html> 这个声明,就会出现一开始的那个错误。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Title</title>
</head>
<body>
     Hello World!
</body>
</html>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值