springboot使用Thymeleaf报错常见的几种解决方案

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot使用Thymeleaf报错常见的几种解决方案

没读过书的孩子   2022-11-24 我要评论

一、问题

我们使用springboot和Thymeleaf的时候可能会报下面的错误:

Exception processing template “index”: An error happened during template parsing (template: “class path resource [templates/index.html]”)
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: “class path resource [templates/index.html]”)

二、解决方法

在springboot后端代码没有写错的前提下:

@Controller
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

1.application.properties配置

注意:一般情况下,问题不会来自于application.properties配置,默认不用配置即可,则选用模板路径为:spring.thymeleaf.prefix=classpath:/templates/

但是如果配置了该选项,修改了默认路径,记得创建对应路径。

2.模板文件头

保证html文件头为:

<!DOCTYPE html>
<html lang="en">

或者选用html约束

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

3.模板文件内容

我们在导入写好的html文件之后一般有几个问题:

1.html文件头约束,也就是上面的问题

2.html标签闭合

3.Thymeleaf的引用需要修改,如一些form表单

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们