Spring welcome-file-list correct mapping

前端 未结 4 1217
攒了一身酷
攒了一身酷 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:28

    See my answer: https://stackoverflow.com/a/15551678/173149 or just:

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
        <url-pattern>/index.htm</url-pattern>    <<==  *1*
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.htm</welcome-file>   <<== *2*
    </welcome-file-list>
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-09 06:36
    @RequestMapping({"/index", "/"})
    

    and

    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>
    

    worked for me.

    0 讨论(0)
  • 2020-12-09 06:36

    Try using

    <welcome-file-list>
      <welcome-file>/index</welcome-file>
    </welcome-file-list>
    
    0 讨论(0)
提交回复
热议问题