SpringBoot 设置默认首页

版权声明: https://blog.csdn.net/woshangniyixiao/article/details/80798940

根据书和网上的综合知识,写下这个,为后来者填坑。

首先最简单的,就是在static目录下,添加index.html文件即可。当你启动项目的时候会自动装配。所以其实根本不需要网上找到的什么配置的方式,画蛇添足。当然了如果想指定一个页面做首页的话, 可以按照如下做:

代码很简单,添加一段这样的代码即可:

package com.travel.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class DefaultView extends WebMvcConfigurerAdapter{

    @Override
    public void addViewControllers(ViewControllerRegistry registry){
        registry.addViewController("/").setViewName("forward:/index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
}

首先说一下,

registry.setOrder(Ordered.HIGHEST_PRECEDENCE);

这里一定要设置成最高级

Ordered.HIGHEST_PRECEDENCE

因为这样哪怕其他controller里有一样的映射(也就是 / )那么根据优先级,也会先加载这个配置。

其次是

forward:/index.html

需要写上全名。

以上。



猜你喜欢

转载自blog.csdn.net/woshangniyixiao/article/details/80798940