45611

What means these mvc:resources mapping tags declared into the servlet-context.xml of a Spring MVC ap

Question:

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

Answer2:

As resources like images, css, javascript etc. are not supposed to be handled by servlet and to be specific by Dispatcherservlet, spring provides a way to specify them using

<blockquote>

mvc:resources

</blockquote>

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>

Recommend

  • how to implement WSDL web services in iphone sdk
  • How to sort the union datastream of flink without watermark
  • python & pandas- Calculation bewteen rows based on certain values in columns from DataFrame
  • TeamCity: Scripting elements jsp:declaration, jsp:expression, jsp:scriptlet are disallowed here
  • macOS Swift Safari-like tabs
  • swift playground error: module 'Python' has no member named 'import'
  • iText Java - add header to an existing pdf
  • Intent-Filter within a Service
  • How to get the remaining session timeout using SessionState?
  • Spring Security sec:authorize tag using Java Config
  • pyinstaller add folder with images in exe file
  • Apache Felix not able to access Postgres JDBC
  • matplotlib and transparency figure
  • Stop the background service after particular time in android
  • Recognize Patterns of images JPG or PNG
  • Texture streaming in DirectX11, Immutable vs Dynamic
  • Scrapy + Selenium + Datepicker
  • Parse and remove special characters in java regex
  • Getting specific color for specific value using JSON data
  • Get an image from the video
  • Azure NodeJS Error: ENOENT, open 'D:\\home\\site\\wwwroot\\bin\\views\\'
  • React/Redux. Access state from other component
  • date changes on export kendoGrid
  • Could not resolve all files for configuration ':react-native-vector-icons:classpath'
  • How do I change the names of buttons on a message box?
  • MayAVI install on Python 3.6 [duplicate]
  • separate tokens in batch file
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • Excel File upload in asp.net using SqlBulkCopy
  • How to resolve this in PHPUnit where it is asking me to set KERNEL_DIR in my phpunit.xml?
  • How do I use libcurl to printf a remote FTP directory listing?
  • gnuplot - How to make zmin equal to zmax keeeping autoscale on z axis
  • Calculate time from document
  • How to turn off notice reporting in xampp?
  • Sign a Pdf using custom digital signature in Java
  • Creating random wired topology for given arbitrary number of nodes on NS2
  • Access to a Matlab gui from the web