Search code examples
javahtmlspringtomcatbackbone.js

Making Spring/Tomcat compatible with HTML5 pushState


I have a single-page web app that's using Backbone.js client routing with pushState. In order to get this to work, I have to tell my server (Java, Spring 3, Tomcat) which URLs should be resolved on the server (actual JSP views, API requets), and which should simply be sent to the index page to be handled by the client. Currently I'm using an InternalResourceViewResolver to simply serve JSP views that match the name of the URL request. Since client-side URLs don't have a view on the server, the server returns a 404.

What is the best way to specify to Spring (or Tomcat) that a few specific URLs (my client-side routes) should all resolve to index.jsp, and anything else should fall through to the InternalResourceViewResolver?


Solution

  • I found that Spring MVC 3 added a tag that does exactly what I need, the mvc:view-controller tag. This got it done for me:

    <mvc:view-controller path="/" view-name="index" />
    <mvc:view-controller path="/admin" view-name="index" />
    <mvc:view-controller path="/volume" view-name="index" />
    

    http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html