Springboot学习 把某一页面设为默认访问页面

步骤一:
首先,在templates文件夹西创建一个文件“index.html”(名字任意取)
在这里插入图片描述
步骤二:创建一个名为“MyMvcConfig.java”的class文件。
在这里插入图片描述
其内容如下:

package com.example.demo.config;


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

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

这样就能直接通过本地ip访问,不用加路径了
在这里插入图片描述

发布了53 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/YUEXILIULI/article/details/103090744