Spring MVC and the <welcome-file> directive

If you use Spring’s awesome MVC framework you will likely encounter one problem that is common among Web frameworks. If the first page you want to open is a jsp page with a Controller – i.e. a page where some java code is executed to prepare data for the invoked jsp…there seems to be no traditional way to do it.

Say you want to be able to access the url http://server.com/ to your servlet container that’s just a directory. It will list the direcoty (if listig is enabled) or just show you a blank page. That is, unless you have defined a <welcome-file> directive in your web.xml which looks for a particular file to load in this case. But this is exactly the problem.

Assume you have defined the following:

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

And the index.html is the formView of, let’s say a SimpleFormController.

Now what your container will do upon a request to its root context is this. It will look for a file in your filesystem called index.html and try to deliver it. Of course your index.html is not really a file, it’s just mapped by, for example, a SimpleUrlHandlerMapping. Game over for the Server. It will not deliver what you expect.

Unfortunately there is no universal way of telling the container to take a look into the Spring context for the url. There is not even a possibility in web.xml to do simple forwards to other urls for the <welcome-file/>.

Instead the solution that seemed most practical to me was, to create a jsp as the <welcome-file>, save it as index.jsp and insert the following code:

<jsp:forward page="index.html" />

I assume that you chose *.html as your url-pattern for the DispatcherServlet here.

3 Responses to “Spring MVC and the <welcome-file> directive”


  1. 1 John Vance

    Thank you very much. I couldn’t find this information stated explicitly anywhere. I’ve been having an issue trying to get acegi working, while hiding the view technology (freemarker) behind controllers so everything is spring managed, and I thought my issue was the defaulttargeturl. I didn’t realize that would not pump through the dispatcher servlet, so my mapping of index.html to indexController etc. was all in vain.

    I’ll pop in index.jsp as you suggest.

  2. 2 John Vance

    That last sentence should read “I didn’t realize that <welcome-file> would not pump through…

    I guess WordPress does tag scrubbing.

  3. 3 ms

    great. thanks. you saved y day

Leave a Reply






Bad Behavior has blocked 1528 access attempts in the last 7 days.

FireStats iconPowered by FireStats