简体   繁体   English

Spring MVC:资源标签理解

[英]Spring mvc:resources tag understanding

In Spring Petclinic the following tag is used for static content . 在Spring Petclinic中,以下标记用于静态内容。

"<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" " “ 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标记后转换后的URL是什么。

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

Any requests from this url pattern /resources/** , Spring will look for /resources/ 这个url模式/resources/**任何请求,Spring都会寻找/resources/

  1. mapping is url pattern mapping是网址格式
  2. location where the resources exists in project class-path 项目类路径中资源所在的location

Configuring Serving of Resources 配置资源服务


Example usages, 用法示例,

  1. By using JSTL <c:url> 通过使用JSTL <c:url>

     <script type="text/javascript" src="<c:url value="/resources/js/jquery.js" />"></script> 
  2. By using <spring:url> 通过使用<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. 如前所述,“位置”是文件所在的位置,“模式”是用于调用文件的URL。

You will understand with this exemple. 您将以这个示例理解。 Suppose we have this WebContent folders structure : 假设我们具有以下WebContent文件夹结构:

-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 : 现在,在spring调度器中,我使用如下标记:

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

then, in my "myview.jsp" to call "myview.css" i have to write this : 然后,在我的“ myview.jsp”中调用“ myview.css”,我必须这样写:

<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. 实际上,Spring调度程序针对所有以“ / reources /”开头的网址,用[mapping =“ / resources / **]表示(以“ resources”开头,而**表示无论结尾如何),它替换了“ / resources /”(由“ / WEB-INF / assets /”(由location =“ / WEB-INF / assets /”配置),然后将其余网址附加到“ / WEB-INF / assets /”以查找位置项目结构中的资源。

I hope it was clear now 我希望现在很清楚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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