初识SpringBoot——设置SpringBoot应用默认首页

一、使用index.html作为欢迎页面
1、静态页面

SpringBoot 项目在启动后,首先会去静态资源路径(resources/static)下查找 index.html 作为首页文件。

在这里插入图片描述

2、动态页面

如果在静态资源路径(resources/static)下找不到 index.html,则会到 resources/templates 目录下找 index.html(使用 Thymeleaf 模版)作为首页文件。

在这里插入图片描述

二、使用其他页面作为欢迎页面
1、静态页面

假设我们在 resources/static 目录下有个 login.html 文件,想让它作为首页。

在这里插入图片描述
一种方式通过自定义一个 Controller 来进行转发:

@Controller
public class HelloController {
    @RequestMapping("/")
    public String hello(){
        return "forward:login.html";
    }
}

另一种方式是通过自定义一个 MVC 配置,并重写 addViewControllers 方法进行转发:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:login.html");
    }
}
2、动态页面

假设我们在 resources/templates 目录下有个 login.html 文件(使用 Thymeleaf 模版),想让它作为首页。

在这里插入图片描述
一种方式通过自定义一个 Controller 来实现,在 Controller 中返回逻辑视图名即可:

@Controller
public class HelloController {
     @RequestMapping("/") 
     public String hello(){
         return "login";
     }
}

另一种方式是通过自定义一个 MVC 配置,并重写 addViewControllers 方法进行映射关系配置即可。

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
    }
}

注意 要想能访问template下的动态页面,必须要引入thymeleaf相关的jar包:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Spring Boot项目中,启动后会首先在静态资源路径(resources/static)下查找index.html作为首页文件。 这意味着如果您在该位置下创建了名为index.html的文件,它将作为您的应用程序的起始页。 另外,如果您需要在开发过程中启用自动重启功能,您可以在配置文件中添加spring.devtools.restart.additional-paths属性,并指定需要监视重启的其他目录路径。这将使您在对这些目录下的文件进行更改后,应用程序可以自动重新启动。 最后,如果您想要跳过Spring核心配置搜索BeanInfo类,您可以在配置文件中添加spring.autoconfigure.exclude属性,并指定需要排除的类。这可以帮助您提高应用程序的启动速度。 请注意,这个属性是可选的,只在您有特定需求的情况下才需要使用。 总而言之,通过在静态资源路径下创建index.html文件作为首页,并根据需要配置自动重启和排除Spring核心配置搜索BeanInfo类,您可以轻松配置Spring Boot的起始页。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [初识SpringBoot——设置SpringBoot应用默认首页](https://blog.csdn.net/chenshufeng115/article/details/108370736)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [springboot配置文件分类](https://blog.csdn.net/qq_43255308/article/details/102896055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值