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);  
    }
}
相關文章
相關標籤/搜索