2016-12-25 31 views

回答

4

Spring Boot documentation

If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configuration class of type WebMvcConfigurerAdapter, but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you can declare a WebMvcRegistrationsAdapter instance providing such components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

你可以這樣做:

@Configuration 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Bean 
    public ViewResolver getViewResolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("/"); 
     resolver.setSuffix(".html"); 
     return resolver; 
    } 
} 

當然,適應前綴,並根據您的實際配置的後綴。


編輯處理重定向頁面時/是要求:

@Configuration 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Bean 
    public ViewResolver getViewResolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("/"); 
     resolver.setSuffix(".html"); 
     return resolver; 
    } 
    // add a mapping for redirection to index when/requested 
    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/").setViewName("forward:/index"); 
    } 
} 
+0

我在根文件夾中的index.html。當我運行localhost:8080時,我想要打開index.html。我如何做到這一點 –

+0

你可以添加一個映射來做重定向。我只是在我的答案中添加了這個來說明如何去做。但是,如果index.html位於根目錄下,通常不需要它。 – davidxxx

+0

但它顯示我這個錯誤:此應用程序沒有明確的映射/錯誤,所以你看到這是一個後備。 –

相關問題