使用springboot在整合使用thymeleaf模板时报错,出错原因应该是使用thymeleaf 模板版本跟springBoot不匹配。

第一种快速解决的方法

直接将@controller或者后面加一个@ResponseBody,其作用是将返回的数据以字符串的方式直接写给浏览器,而不是跳转到某个页面。
或者直接将@controller改为@RestController,这是spring新出的注解,相当于将@controller和@ResponseBody整合到一起。

第二种方式

更改thymeleaf版本使其与springBoot版本匹配就行。
起初pom.xml的配置是这样

<!--Thymeleaf-->
		<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
		<dependency>
			<groupId>org.thymeleaf</groupId>
			<artifactId>thymeleaf</artifactId>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-java8time -->
		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-java8time</artifactId>
		</dependency>

在后面增加下面这段代码再一运行就能成功了

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐