springboot如何配置直接跳转到html页面

本文介绍了在SpringBoot项目中如何配置以直接访问HTML页面。首先,需要在pom.xml添加相关依赖并解决未安装的包问题。接着,在application.properties进行配置,并在templates目录下创建index.html文件。然后创建一个@Controller注解的类,避免使用@RestController导致返回字符串。最后,运行项目,通过浏览器访问localhost:8080/index即可成功显示页面。

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

1、在pom.xml里面加入

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

 我发现我的包是红色的,原来是没有导入依赖啊

 

右侧有这些没安装依赖的包,右击下载就可以了

2、在application.properties里面配置

# 定位模板的目录
spring.thymeleaf.prefix=classpath:/templates/

 

在templates下建立index.html文件

 

新建一个类

 @controller不是@RestController,使用@RestController会返回"index"字符串

package com.example.demo.control;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexControl {
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}

 

 运行项目

浏览器上输入localhost:8080/index

 

成功了 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值