方式一:
1、html文件放在resource下的static目录下
2、配置试图解析器#配置视图解析器
spring:
  mvc:
    view:
      prefix: /
      suffix: .html

方式二:
1、html文件放在resource下的templates目录下
2、配置文件
        spring:
          thymeleaf:
            prefix:
              classpath: /templates   # 访问template下的html文件需要配置模板,映射
            cache: false              # 开发时关闭缓存,不然没法看到实时页面
3、添加依赖:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>


Controller用@Controller注解代替@RestController
@RestController注解,相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面

@GetMapping("/")
public String index(){
    return "index";
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐