springboot不经过controller直接访问html页面

自定义视图映射可以解决这个问题,默认情况下template中的静态页面无法直接通过URL访问,需要通过controller的跳转,定义映射之后,可以将直接访问的URL映射成类似controller的跳转功能。

//扩展配置功能
@Configuration
public class MyConfigMvc extends WebMvcConfigurerAdapter {

    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            /*视图映射功能*/
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                //视图映射:浏览器发送"/"请求,会来到index页面(thymeleaf解析的页面),
                registry.addViewController("/").setViewName("/index");
                registry.addViewController("/index.html").setViewName("/index");
                registry.addViewController("/home.html").setViewName("/index");
                //配置springboot直接访问静态html页面,不经过controller
                //配置之后,发送/loginAndRegister.html,就相当于在controller中return "loginAndRegister"
                registry.addViewController("/loginAndRegister.html").setViewName("/loginAndRegister");
            }
        };
        return adapter;
    }
}
原文地址:https://www.cnblogs.com/sstealer/p/13498640.html