Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 332
John Farrelly Avatar asked Apr 19 '12 21:04

John Farrelly


2 Answers

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.

like image 156
BalusC Avatar answered Oct 18 '22 00:10

BalusC


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

like image 26
Bozho Avatar answered Oct 18 '22 00:10

Bozho