一、问题

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

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表单

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

原文地址:https://blog.csdn.net/qq_38278799/article/details/103828420

相关文章: