welcome-file in web.xml with spring not working?
Solution 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
Solution 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.
Solution 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
John Farrelly
Updated on June 06, 2022Comments
-
John Farrelly about 2 years
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>
-
John Farrelly about 12 yearsHi Bozho. Are you sure? The documentation docs.oracle.com/cd/E13222_01/wls/docs81/webapp/… makes it seem like directories should be supported.
-
Bozho about 12 yearsmight be the way they implemented it in WebLogic. But Jetty are probably interpreting the standard differently. Try it in a completely new application, without spring, just to test
-
John Farrelly about 12 yearsIt worked in a new spring application. However, the difference being that I had real files in real directories, instead of pointing to servlets. Perhaps that is the issue...
-
John Farrelly about 12 yearsHi Ryan. That's what I'm doing at the moment, but unfortunately it only works at the root of the context, and not in subdirectories.
-
John Farrelly about 12 yearsCouldn't find a way to solve it, so I just had to append index.page to all my URLs. Tedious work!
-
BalusC about 12 yearsThe welcome file is not only about the root of the context.
-
John Farrelly about 12 yearsHi BalusC. There is a special parameter in webdefault.xml in Jetty called welcomeServlets. If you set it to true, then Jetty will look for a servlet that satisfies the welcome-file if a physical file does not exist. I have that set to true, and it worked for the root context, but does not work for subdirectories unfortunately.
-
Kal over 11 yearsCreating an empty file to trick the container is a bit of genius. Thanks!
-
Koray Tugay over 6 yearsYou are a genius.