1. 如果不作任何配置,在static或templates文件夹中放入index.html,会自动跳转index.html
原因:
(4条消息) Spring Boot 默认跳转到index.html的二种方式_NULL-优快云博客_springboot 默认index.html
2. 在controller中配置
@GetMapping("/")
public String index(){
return "index";
}
应该使用@Controller而非@RestfulController,并且index.html需要放在templates文件夹中
3. 参考:(4条消息) Spring Boot 默认跳转到index.html的二种方式_NULL-优快云博客_springboot 默认index.html
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
/**
* 拦截某个请求跳转固定位置
*
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//super.addViewControllers(registry);
registry.addViewController("/nihao").setViewName("success");
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
}