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?
My way of solving this problem is placing all your actions with a specific prefix like "web" or "service" and configure that all url's with that prefix will be intercepted by the DispatcherServlet.
I got the same problem and found Joris's answer very helpful. But additionally I need to add
to the servlet config file. Without that resource mapping will not work and all handlers will stop working. Hope this will help someone.
This did the real job in my case
in web.xml:
...
I used both ways that is urlrewrite and annotation based in spring mvc 3.0.x and found that annotation based approach is most suitable that is
So annotation based approach will be the good deal.
I solved it this way:
This works on Tomcat and ofcourse Jboss. However in the end I decided to use the solution Spring provides (as mentioned by rozky) which is far more portable.
I just add three rules before spring default rule (/**) to tuckey's urlrewritefilter (urlrewrite.xml) to solve the problem