welcome-file in web.xml with spring not working?

2019-06-22 14:57发布

问题:

I have setup my spring-mvc servlet to match *.page requests. I have setup the welcome-file-list in web.xml to be index.page

This works when I go to the root of my webserver:

http://me.com does get redirected to http://me.com/index.page correctly.

However, it doesn't redirect when I use subdirectoris:

http://me.com/dashboard does not get redirected to http://me.com/dashboard/index.page

Is there any way to get this mapping working?

My web.xml file (extract):

<welcome-file-list>
    <welcome-file>index.page</welcome-file>
</welcome-file-list>

<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>*.page</url-pattern>
</servlet-mapping>

My webdefault.xml (from jetty):

    <init-param>
        <param-name>dirAllowed</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>welcomeServlets</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>redirectWelcome</param-name>
        <param-value>false</param-value>
    </init-param>

回答1:

It will work only for real, physical directories, not won't work for arbitrary servlet mappings simulating directory structure.

Spring MVC allows very complex URL mappings, so you'd better handle this scenario with @RequestMapping



回答2:

The <welcome-file> should represent a physically existing file in an arbitrary folder which you would like to serve whenever the enduser requests a folder (such as the root /, but it can also be any other folder such as /foo/). You only need to understand that the servletcontainer will test its physical existence before performing a forward, if it does not exist then a HTTP 404 page not found error will be returned.

In your particular case, you do not have a physical index.page file in your root folder. You have actually a index.jsp file in your root folder. The index.page is merely a virtual URL. So the servletcontainer won't be able to find the physical index.page file and hence error out with a 404.

You can workaround this by fooling the servletcontainer by placing a physically existing index.page file next to the index.jsp file in the same folder. That file can just be kept completely empty. The servletcontainer will find the file and then forward to index.page which will then invoke the controller servlet which in turn will actually serve the index.jsp as view. That'll work just fine.



回答3:

To avoid forwarding welcome file itself, its better add a mapping for it.

  <servlet-mapping>
     <servlet-name>spring-mvc</servlet-name>
     <url-pattern>index.html</url-pattern>
  </servlet-mapping>

And 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



回答4:

This is something you need to probably set in your web server, and so, maybe server specific

For Apache HTTP Server you can achieve this by setting the DirectoryIndex directive like so: DirectoryIndex index.page

Apparently, someone has already asked this question, and has accepted an answer at web.xml default file in directory (for jetty, or tomcat)? - See if it works for you.