springboot中使用Thymeleaf报错常见几种解决思路

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

发布了76 篇原创文章 · 获赞 44 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_38278799/article/details/103828420