I've mapped the Spring MVC dispatcher as a global front controller servlet on /*
.
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
However, this mapping stops the access to static files like CSS, JS, images etc which are all in the /res/
folder.
How can I access them anyway?
As of 3.0.4 you should be able to use
mvc:resources
in combination withmvc:default-servlet-handler
as described in the spring documentation to achieve this.http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources
The best way to handle this is using some kind of URL re-writing. In this way, you can have clean restful URLs, and NOT with any extensions i.e abc.com/welcom/register as opposed to abc.com/welcome/resister.html
I use Tuckey URL which is pretty cool.
It's got instructions on how to set up your web app.I have set it up with my Spring MVC web app. Of course, everything was fine until I wanted to use annotations for Spring 3 validations like
@Email
or@Null
for domain objects.When I add the Spring mvc directives:
.. it breaks the good ol Tuckey code. Apparently,
< mvc:default-servlet-handler />
replaces Tuckey, which I'm still trying to solve.I've run into this also and never found a great solution. I ended up mapping my servlet one level higher in the URL hierarchy:
And now everything at the base context (and in your /res directory) can be served up by your container.
Serving static content with appropriate suffix in multiple servlet-mapping definitions solved the security issue which is mentioned in one of the comments in one of the answers posted. Quoted below:
which helped me a lot. And here is how I solved it:
I found that using
in the spring MVC servlet bean definition file works for me. It passes any request that isn't handled by a registered MVC controller on to the container's original default handler, which should serve it as static content. Just make sure you have no controller registered that handles everything, and it should work just fine. Not sure why @logixplayer suggests URL rewriting; you can achieve the effect he's looking for just adequately using Spring MVC alone.