As said by @Nancom
<mvc:resources location="/resources/" mapping="/resource/**"/>
So for clarity lets our image is in
resources/images/logo.png"
The location attribute of the mvc:resources tag defines the base directory location of static resources that you want to serve. It can be images path that are available under the src/main/webapp/resources/images/
directory; you may wonder why we have given only /resources/ as the location value instead of src/main/webapp/resources/images/
. This is because we consider the resources
directory as the base directory for all resources, we can have multiple sub-directories under resources
directory to put our images and other static resource files.
The second attribute, mapping, just indicates the request path that needs to be mapped to this resources
directory. In our case, we have assigned /resource/**
as the mapping value. So, if any web request starts with the /resource
request path, then it will be mapped to the resources
directory, and the /**
symbol indicates the recursive look for any resource files underneath the base resources
directory.
So for url like
http://localhost:8080/webstore/resource/images/logo.png
. So, while serving this web request, Spring MVC will consider /resource/images/logo.png
as the request path. So, it will try to map /resource
to the base directory specified by the location attribute, resources
. From this directory, it will try to look for the remaining path of the URL, which is /images/logo.png
. Since we have the images
directory under the resources
directory, Spring can easily locate the image file from the images
directory.
So
<mvc:resources location="/resources/" mapping="/resource/**"/>
gives us for given [requests] -> [resource mapping]:
http://localhost:8080/webstore/resource/images/logo.png
-> searches in resources/images/logo.png
http://localhost:8080/webstore/resource/images/small/picture.png
-> searches in resources/images/small/picture.png
http://localhost:8080/webstore/resource/css/main.css
-> searches in resources/css/main.css
http://localhost:8080/webstore/resource/pdf/index.pdf
-> searches in resources/pdf/index.pdf