spring boot直接返回静态html

spring boot直接返回静态html

一般spring boot的通常教程的例子都是经过模板来返回页面,好比thymeleaf或者freemarker,可是直接返回html的例子比较少。本文参考文章SpringBoot : How to display static html file in Spring boot MVC application。说明如何让spring boot直接返回html。css

通常来讲resources/static或者resources/public文件夹能够用来提供js,css,图片等文件访问。不通过配置,直接返回html会报404错误。提供静态html访问主要须要以下配置(懒得翻译了。。。)html

  • You should create a class that extends WebMvcConfigurerAdapterspring

  • Your class should have @Configuration annotation.springboot

  • You class should not have @EnableMvc annotation.mvc

  • Override addViewControllers method and add your mapping.app

  • Override configurePathMatch method and update suffix path matchingide

其实,添加以下配置类就行了翻译

@Configuration  
public class MvcConfigurer extends WebMvcConfigurerAdapter {  
  
    @Override  
    public void addViewControllers(ViewControllerRegistry registry) {  
        registry.addViewController("/error").setViewName("error.html");  
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);  
    }
    
    @Override  
    public void configurePathMatch(PathMatchConfigurer configurer) {  
        super.configurePathMatch(configurer);  
        configurer.setUseSuffixPatternMatch(false);  
    }
}
相关文章
相关标签/搜索