Spring welcome-file-list correct mapping

前端 未结 4 1239
攒了一身酷
攒了一身酷 2020-12-09 05:57

I know that in spring I must define welcome-file, which should be outside of WEB-INF folder, so I define it like this:

web.xml:



        
4条回答
  •  孤街浪徒
    2020-12-09 06:29

    In case of java configuration you can override two methods in class that extends WebMvcConfigurerAdapter

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/index");
    }
    
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
    

    If you wanna serve index.html explicitly, turn it into a resource override a method in the same class as below:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/index.html").addResourceLocations("/WEB-INF/views/index.html");
    }
    

    Of course addResourceLocations must follows the folder choosen to hold your views.

    See these samples

提交回复
热议问题