1. 创建类实现WebMvcConfigurer,并添加@Configuration注解
SpringBoot的默认首页是index.html,只要在static创建index.html文件即可直接采用IP+端口访问。此处修改目的就是为了IP+端口访问自定义的首页,而不是默认的index.html。
SpringBoot 1.x版本的WebMvcConfigurerAdapter已失效,不合适使用。
如果用继承WebMvcConfigurationSupport的方式,会使默认的spring mvc配置失效。
package com.errmao.waterloo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 设置默认访问界面
*/
@Configuration
public class WelcomeViewConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/")
.setViewName("login");
}
}
2. 配置application.properties
login.html可以放在static目录下。
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
3. 登陆测试
使用IP+端口访问,访问的是login.html,修改成功。