I am developing a webapp using Spring MVC 3 and have the DispatcherServlet
catching all requests to '/' like so (web.xml):
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Now this works as advertised, however how can I handle static content? Previously, before using RESTful URLs, I would have caught all *.html for example and sent that to the DispatcherServlet
, but now it's a different ball game.
I have a /static/ folder which includes /styles/, /js/, /images/ etc and I would like to exclude /static/* from the DispatcherServlet
.
Now I could get static resources working when I did this:
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/</url-pattern>
</servlet-mapping>
But I want it to have nice URLs (the point of me using Spring MVC 3) not the landing page being www.domain.com/app/
I also don't want a solution coupled to tomcat or any other servlet container, and because this is (relatively) low traffic I don't need a webserver (like apache httpd) infront.
Is there a clean solution to this?
Best Answer
Since I spent a lot of time on this issue, I thought I'd share my solution. Since spring 3.0.4, there is a configuration parameter that is called
<mvc:resources/>
(more about that on the reference documentation website) which can be used to serve static resources while still using the DispatchServlet on your site's root.In order to use this, use a directory structure that looks like the following:
The contents of the files should look like:
src/springmvc/web/HelloWorldController.java:
WebContent/WEB-INF/web.xml:
WebContent/WEB-INF/springmvc-servlet.xml:
WebContent/jsp/index.jsp:
Hope this helps :-)