java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot项目中访问HTML页面

SpringBoot项目中实现访问HTML页面

作者:nanxun___

文章介绍了Spring Boot项目中首页的配置方法,包括将`index.html`作为首页面、使用自定义`xxx.html`作为首页面以及通过Controller控制层返回任意HTML页面,同时,文章还强调了在`pom.xml`中添加依赖的重要性,并详细说明了使用`@Controller`注解来渲染页面

一、将 index.html 作为首页面

1.静态首页

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

2.动态首页

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

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

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

@RestController
public class IndexController {
    @RequestMapping("/")
    public String hello(){
        System.out.println("OK");
        return "test";
    }
}

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

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

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

2.书写Controller层

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文