How to exclude css and js files from Spring MVC dispatcher mapping
After setting up basics for the Spring MVC dispatcher, I included the CSS file but without any results. First I thought the path isn’t declared correctly (I used relative path for the first time). Then I tried with contextPath:
but still no changes on the page. After I tried to access this css file directly in the browser I’ve noticed the following in my tomcat console:
jan 04, 2013 1:25:34 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping found for HTTP request with URI [/beunique/css/main.css] in DispatcherServlet with name 'mvc-dispatcher'
Next thing I wanted to do is to exclude the css and js folders from Spring MVC mapping.
<!-- Default mapping for css and js folders --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/css/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/js/*</url-pattern> </servlet-mapping>
Now, after restarting tomcat, you should be able to access your files in the css and js folders. If you want, you can map the files by extension, but I think this is better approach, because you’ll want to map the images folder too, and if you map the files by extension, you will have to add mapping for every image extension (.jpg, .png, .gif etc).