简体   繁体   English

在Spring项目中引用CSS文件

[英]Referring css files in Spring project

I'm referring my resources in my Spring project as below 我在下面的Spring项目中引用了我的资源

dispatcher-servlet.xml dispatcher-servlet.xml

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

jsp files jsp文件

<script src="<c:url value="/resources/js/jquery-1.11.1.min.js" />"></script>
        <script src="<c:url value="/resources/js/startpage.js" />"></script>
        <link href="<c:url value="/resources/css/startpage.css" />" rel="stylesheet">

Now, I have a "image" folder within the resources folder and I want to refer few images in my css files. 现在,我在resources文件夹中有一个“ image”文件夹,我想在css文件中引用几个图像。

How can I refer that? 我怎么可以这样呢?

You could put img tag to your .jsp file 您可以将img标签放入.jsp文件中

<img src="${pageContext.servletContext.contextPath}/resources/image/picture1.png" alt="" title="" />

edit1. 编辑1。 In the css file 在css文件中

#yourcssclass {
width: 46px;
height: 44px;
background: url(/resources/image/picture1.png) 0 0;
}

Its all about relativity. 一切都与相对性有关。

Lets say your resource folder is broken down into: -image -js -css 可以说您的资源文件夹分为:-image -js -css

If you have file in css test.css you wish to edit to include image from image folder, you need to do something like following. 如果您要编辑css test.css中的文件以包含图像文件夹中的图像,则需要执行以下操作。

#testdiv {
background-image: url('../image/testimage.jpg')
}

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

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