Spring Boot 默认页面

Spring Boot 默认的欢迎界面是 main/resources/static/index.html 。

修改 Spring Boot 默认页面

原先的 WebMvcConfigurerAdapter 已过时了,改为继承 WebMvcConfigurationSupport 类 或 实现 WebMvcConfigurer 接口。

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.WebMvcConfigurationSupport;

@Configuration
public class DefaultViewConfig extends WebMvcConfigurationSupport {

    @Override
    public void addViewControllers(ViewControllerRegistry registry){
        //设置访问路径为 “/”
        registry.addViewController("/").setViewName("login.html");
        //设置为最高优先级
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
}

通过编写配置类,继承 WebMvcConfigurationSupport 类,重写 addViewController( ) 方法。

registry.addViewController("/").setViewName("login.html");

该段代码相当于

@Controller
public class TestController{
    @RequestMapping("/")
    public String testController(){
        return "login";
    }
}

浏览器中访问 http://localhost:8080/ 可访问 main/resources/static/login.html 。