I am pretty new in Spring and I am working on a Spring MVC application.
Into the <strong>servlet-context.xml</strong> configuration file I found these lines:
<!-- resources exclusions from servlet mapping --> <mvc:resources mapping="/assets/**" location="classpath:/META-INF/resources/webjars/"/> <mvc:resources mapping="/css/**" location="/css/"/> <mvc:resources mapping="/img/**" location="/img/"/> <mvc:resources mapping="/js/**" location="/js/"/>
The comment say <strong>resources exclusions from servlet mapping</strong>.
What exactly means these lines? I know that this <strong>servlet-context.xml</strong> should contain the <strong>DispatcherServlet</strong> configuration that handle the MVC behavior (and it is pretty clear for me) but what exactly does the previous lines?
Can you explain me in details the meaning and the previous syntax?Answer1:
If you consider a spring mvc configuration where all the requests are mapped to a
DispatcherServlet, you can categorize those requests as requests for static and dynamic resources.
The requests for dynamic resources are matched by what you program inside your controller methods, and they are the subject of the typical framework processing such as path matching, content negotiation, validation, binding, conversion, formating, argument resolving.
The requests for static resources are the requests for .js, .css, or some other resources that are not getting created rather already exist deployed with your application. They are not handled by the programmatic controller methods rather by the <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.html" rel="nofollow">
ResourceHttpRequestHandler</a>, simply because they have a completly different set of processing actions comparing to dynamic request (apart from path matching). You can define the location of static files for the given mapping (this can be the classpath, some other webapp folder or a file system), caching strategy for the resource, transformations (such as modyfing links in css, tranforming LESS to CSS)
So its not really that you don't want the static resources to be handled by the servlet, in fact you can get a lot of possibilities and flexibility by doing so, check for example <a href="https://spring.io/blog/2014/07/24/spring-framework-4-1-handling-static-web-resources" rel="nofollow">handling static web resources</a> it is just that static and dynamic requests are a subject of different kind of actions, and by using
mvc:resources tag, you designate by mappings which requests are to be handled as static requests
tag. If such resource is not mapped using above tag, requested path will be used by dispatcher servlet to find a controller with matching request mapping. But with the tag, spring looks into location attribute of tag to find and return the resource and if the resource is not found, HTTP status code 404 is returned.
Other helpful answers<ol><li>
<a href="https://stackoverflow.com/questions/5379054/spring-mvc-mvcresources-location-attribute?rq=1" rel="nofollow">Nice explanation of location attribute of the tag here</a></li> <li>
<a href="https://stackoverflow.com/questions/8195213/spring-3-mvc-resources-and-tag-mvcresources" rel="nofollow">spring-3-mvc-resources-and-tag-mvcresources</a></li> </ol>