首页 > 技术文章 > spring boot 项目设置默认访问路径方法

kevliudm 2019-08-09 11:42 原文

spring boot项目一般通过Application启动,且不需要配置web.xml,所以设置默认访问页面可以通过以下方法实现,比如增加默认DefaultView类,代码如下:

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 reg) {
    	reg.addViewController("/").setViewName("login");//默认访问页面
        reg.setOrder(Ordered.HIGHEST_PRECEDENCE);//最先执行过滤
        super.addViewControllers(reg);
    }
}

推荐阅读