Spring boot遇到的thymeleaf一些报错问题

本文介绍如何在Spring Boot项目中正确配置Thymeleaf模板引擎,并解决因版本不匹配导致的HTML解析错误问题。

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

一、pom.xml文件中添加boot的依赖

    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

此时启动会报错:

org.xml.sax.SAXParseException: 元素类型 “link” 必须由匹配的结束标记 “” 终止,org.xml.sax.SAXParseException: 元素类型 “meta” 必须由匹配的结束标记 “” 终止。

解决方法是在pom.xml 文件中指定具体的thymeleaf 版本 

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version>
</properties>

此时启动,访问html依旧报错:

org.xml.sax.SAXParseException: 元素类型 "meta" 必须由匹配的结束标记 "</meta>" 终止。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" >
    <title>Title</title>
</head>
<body>
    nihao
</body>
</html>

thymeleaf模板引擎默认是Template modes:HTML5解析的,所以解析比较严格。必须写上反斜杠 /
<head>
    <meta charset="UTF-8" />
    <title>Title</title>
</head>



### 解决Spring BootThymeleaf组合使用时出现的500错误 当遇到HTTP状态码500内部服务器错误时,这通常意味着应用程序遇到了意外情况,无法完成请求。对于Spring BootThymeleaf集成环境中的此类问题,可以从多个角度排查并解决问题。 #### 配置文件检查 确保`application.properties`或`application.yml`中正确设置了Thymeleaf的相关属性。例如: ```properties spring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 这些设置有助于排除模板路径不匹配等问题[^1]。 #### 错误处理机制优化 为了更好地捕获异常信息,在控制器层面上可以通过实现全局异常处理器来增强应用对未预见状况的响应能力。定义一个类继承自`ResponseEntityExceptionHandler`,从而覆盖默认行为,并提供更友好的反馈给前端页面。 ```java @ControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(Exception.class) public ModelAndView handleAllExceptions(Exception ex) { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", "An unexpected error occurred."); modelAndView.setViewName("error"); return modelAndView; } } ``` 此外,如果希望调整绑定失败后的消息编码方式,则可通过修改`spring.mvc.message-codes-resolver-format`属性值为`PREFIX_ERROR_CODE`或`POSTFIX_ERROR_CODE`,让框架按照指定格式生成相应的错误提示语句[^2]。 #### Bean冲突检测 有时由于版本差异或其他原因可能导致某些Bean被重复注册而引发冲突,进而造成服务端抛出500错误。此时应仔细审查依赖库之间的兼容性以及是否存在不必要的第三方插件干扰正常流程;必要情况下可尝试禁用自动装配功能(`@EnableAutoConfiguration(exclude={...})`)或将特定bean显式声明为primary优先级以规避潜在风险[^3]。 通过上述措施能够有效降低因配置不当所引起的运行期故障概率,提高系统的稳定性和用户体验度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值