Spring welcome-file-list正确映射

我知道在spring我必须定义welcome-file,它应该在WEB-INF文件夹之外,所以我这样定义:

web.xml中:

 index.jsp   spring /  

但实际上我的真实代码是在WEB-INF / jsp / contact.jsp中

所以我总是这样做:

  

在我的控制器中,这意味着:

 @RequestMapping("/index") public String listContacts(Map map) { map.put("contact", new Contact()); map.put("contactList", contactService.listContact()); return "contact"; } 

我怎么能这样做,欢迎文件总是进入我的索引映射,这导致contact.jsp?

如果这令人困惑,请随意提问…

 @RequestMapping({"/index", "/"}) 

    

为我工作。

请参阅我的回答: https : //stackoverflow.com/a/15551678/173149或者只是:

  dispatcher / /index.htm <<== *1*   index.htm <<== *2*  

在java配置的情况下,您可以覆盖扩展WebMvcConfigurerAdapter的类中的两个方法

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

如果您想明确地为index.html提供服务,请将其转换为资源覆盖同一类中的方法,如下所示:

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

当然addResourceLocations必须在选择的文件夹后面保存您的视图。

见这些样本

尝试使用

  /index