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
成功了