Springboot使用thymeleaf进行页面跳转

本文介绍如何在Spring Boot项目中配置并使用Thymeleaf模板引擎,包括添加依赖、编写控制器代码及创建HTML模板文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要修改的文件

1.在porm文件中添加依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
			<version>2.0.6.RELEASE</version>
		</dependency>

2.java代码

@Controller
public class IndexController {
   @RequestMapping("/vm")
    public String news(){
        return "hello";//hello是页面的名字,用hello.html也可以
    }
}

3.在src/main/resources/templates中创建一个hello.html文件用于显示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Title</title>
</head>
<body>
<h3>hello themeleaf</h3>
</body>
</html>

4.(可选)如果按上面步骤还不行的话可以试试在application.properties文件中添加

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.cache=false
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值