0


SpringBoot项目中如何访问HTML页面

一.将 index.html 作为首页面

1.静态首页

springboot项目启动后,默认会到静态资源 resources->static 目录下查找index.html页面

2.动态首页

在静态资源路径找不到 index.html 文件,会到 resources->templates 目录下找 index.html

二.使用自定义 xxx.html 作为首页面

1.方法一:通过Controller控制首页

  1. @RestController
  2. public class IndexController {
  3. @RequestMapping("/")
  4. public String hello(){
  5. System.out.println("OK");
  6. return "test";
  7. }
  8. }

三.用Controller控制层返回任意html页面

1.在pom.xml文件中添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>

2.书写Controller层

注解要写@controller而不是@restController,前者用来渲染页面,后者用来返回数据

标签: spring boot java 后端

本文转载自: https://blog.csdn.net/nanxun___/article/details/140584921
版权归原作者 nanxun___ 所有, 如有侵权,请联系我们删除。

“SpringBoot项目中如何访问HTML页面”的评论:

还没有评论