为什么springboot不能直接访问到html
第一种方法
在application.yml中声明静态资源的位置
spring:
resources:
static-locations: classpath:templates/
第二种方法
我还没有掌握!!!但是觉得会有其他用处(知道的评论区可以交流交流)
//扩展配置功能
@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;
}
}