2016-12-25 73 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

但它显示我这个错误:此应用程序没有明确的映射/错误,所以你看到这是一个后备。 –

相关问题