SpringBoot访问静态视图的两种方式

## 1 通过Controller控制跳转

package com.syu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

  /*  @RequestMapping({"/","index.html"})
    public String index(){
        return "index";
    }*/
}

## 2 通过 WebMvcConfigurer

package com.syu.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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");
    }

}
原文地址:https://www.cnblogs.com/lotus-wmm/p/12636708.html