简体   繁体   中英

Spring mvc:resources tag understanding

In Spring Petclinic the following tag is used for static content .

"<mvc:resources mapping="/resources/**" location="/resources/"/> "

But I am facing problems in understanding this because the mapping and the location are same . So what purpose will this tag solve ?If the css file request is like.

"spring:url value="/resources/css/petclinic.css" var="petclinicCss" "

then what would be the converted URL after mvc:resources tag has been executed .

<mvc:resources mapping="/resources/**" location="/resources/"/>

Any requests from this url pattern /resources/** , Spring will look for /resources/

  1. mapping is url pattern
  2. location where the resources exists in project class-path

Configuring Serving of Resources


Example usages,

  1. By using JSTL <c:url>

     <script type="text/javascript" src="<c:url value="/resources/js/jquery.js" />"></script> 
  2. By using <spring:url>

     <spring:url value="/resources/images/favicon.ico" var="favicon" /> 

As sayed, the "location" is where your files are and "pattern" the URL used to call them.

You will understand with this exemple. Suppose we have this WebContent folders structure :

-WebContent
    -META-INF
    -WEB-INF
        -assets
            -css
                *myview.css
            -js
        -view
            *myview.jsp //or html or any kind of view format)

now, in spring dispatcher I use the tag like this :

<mvc:resources mapping="/resources/**" location="/WEB-INF/assets/" />

then, in my "myview.jsp" to call "myview.css" i have to write this :

<link href="<c:url value="/resources/css/myview.css" />" rel="stylesheet" type="text/css">

In fact, what Spring dispatcher do is for all url starting with "/reources/", expressed by [mapping="/resources/**] (start with "resources" and ** means no matter the end) it replace the "/resources/" by "/WEB-INF/assets/" (configured by location="/WEB-INF/assets/") and append the rest of the url to "/WEB-INF/assets/" to fin the location of the resources in the project structure.

I hope it was clear now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM