【问题标题】:Unable to access Image files in CSS "background: url(image_path)" attribute in a Spring MVC Application无法在 Spring MVC 应用程序中访问 CSS“背景:url(image_path)”属性中的图像文件
【发布时间】:2015-08-13 12:03:42
【问题描述】:

我有一个 Spring MVC 应用程序,我试图从中访问 CSS 文件的 background: url(image_path) 属性中的图像。

CSS 工作得很好,但图像没有显示出来。我认为解决图像文件有问题。

我尝试了绝对寻址和相对寻址来获取图像,但它不起作用。

background-image: url("resources/images/forIndex/bg1.jpg");

我也试过了:

background-image: url("../images/forIndex/bg1.jpg");

我的webapp包如下:

style.css:

#home {
 background:
    url(../images/forIndex/bg1.jpg);
 background-size: cover;
 background-position: center;
 background-repeat: no-repeat;
 background-attachment: fixed;
 height: auto;
 min-height: 710px;
 padding-top: 60px;
 color: #ffffff;
}

#overview-video {
 background: url(resources/images/forIndex/bg2.jpg);
 background-size: cover;
 background-position: center;
 background-repeat: no-repeat;
 background-attachment: scroll;
 text-align: center;
}

mvc-dispatcher-servlet.xml:

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:default-servlet-handler />
<mvc:annotation-driven />

web.xml:

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml
    /WEB-INF/applicationContext*.xml 
    /WEB-INF/spring-security-servlet.xml</param-value>
</context-param>

<!-- Mapping for images -->
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/images/*</url-pattern>
</servlet-mapping>

任何帮助将不胜感激。谢谢!!

【问题讨论】:

  • 有人,请帮忙:)

标签: css spring-mvc


【解决方案1】:

您不需要为 web.xml 中的图像映射 servlet,因为它将通过调度程序 servlet。

否则配置看起来没问题。在像 www.mysite.com/index.html 这样的网址上,您应该能够像这样访问图像:

background-image: url("resources/images/forIndex/bg1.jpg")

【讨论】:

    猜你喜欢
    • 2015-05-23
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2020-06-30
    • 2015-07-06
    • 1970-01-01
    相关资源
    最近更新 更多