springboot开发中thymeleaf模板报错template parsing

如题,模板一直报错:
An error happened during template parsing (template: “class path resource [templates/index.html]”)

尝试:

  1. 在index.html中将thymeleaf模板注释掉,只剩一般的html,可以正常运行,那就应该是thymeleaf出了问题
  2. 尝试加入最简单的thymeleaf语句,<span th:text="hello world"></span>,打印hello world字符串,报相同的错误

解决办法:

将尝试语句改成了, <span th:text="'hello world'"></span>,字符串另外加上单引号括起来。
如此证明我的环境配置没有问题,就是thymeleaf语法写错了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Spring Boot框架时,我们可以使用Thymeleaf模板引擎来渲染HTML页面。Thymeleaf是一种面向服务器的模板引擎,它可以用来构建与Web标准兼容的HTML5页面。 在Spring Boot,我们首先需要在pom.xml文件添加Thymeleaf依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 在Spring Boot应用程序,我们需要配置Thymeleaf的视图解析器。可以在application.properties文件添加以下属性: ``` spring.thymeleaf.enabled=true spring.thymeleaf.cache=false ``` 可以看到,我们打开了Thymeleaf的缓存机制,以便更方便地测试HTML页面的更改。然后,我们需要在我们的Spring Boot应用程序创建一个控制器,以便为HTML页面提供服务: ``` @Controller public class MyController { @GetMapping("/") public String index(Model model) { model.addAttribute("message", "Hello, Thymeleaf!"); return "index"; } } ``` 在上面的控制器,我们使用@GetMapping注释来将应用程序映射到“/”路径。然后,我们将一个消息添加到Model,以便将其传递给Thymeleaf视图。最后,我们返回视图名称“index”,这将是我们要渲染的HTML页面。 在Thymeleaf模板,我们可以使用th:text属性来将消息添加到HTML页面: ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Hello, Thymeleaf</title> </head> <body> <h1 th:text="${message}">Hello, World!</h1> </body> </html> ``` 在上面的示例,我们使用th:text属性来向页面添加消息。可以看到,我们使用了Thymeleaf的EL表达式来获取控制器传递的消息。 使用了这些步骤后,当我们访问应用程序的根目录时,会渲染index.html页面,并将消息“Hello, Thymeleaf!”添加到页面。这就是如何在Spring Boot应用程序使用Thymeleaf模板引擎来渲染HTML页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值