I’m surprised about how far Spring has come without ever adressing simple everyday problems. If you really, honestly want to use Spring for your application all the way through, be prepared for some good shockers. Want some spring oddities?
Continue reading ‘Pain is the cost of Spring MVC’s elegance’
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.