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

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

 

成功了 

发布了123 篇原创文章 · 获赞 4 · 访问量 5663

猜你喜欢

转载自blog.csdn.net/tian_jiangnan/article/details/105690821