html - 为什么图像的 URL 在 css 中不起作用?

标签 html css image jsp url

我在这个问题上花了一整天的时间,并用谷歌搜索了许多答案。

我的 login.css 文件位于:

WebContent/resources/css/login.css

我的图像文件位于:

WebContent/resources/img/pencil.jpg

当我尝试通过此 CSS 将图像添加到正文背景时:

body{
    background-image:url("../img/pencil.jpg") center center no-repeat;
}

没用! 另外,我尝试了另一种方法:我尝试不通过 CSS 直接将另一个图像添加到我的 JSP 页面。 我的 JSP 位于:

  WebContent/WEB-INF/views/login.jpg

我的图片位于:

 WebContent/resources/img/calendar.png

JSP 中的代码是:

<img src="../../resources/img/calendar.png"/>

他们都失败了!其他一切正常,只是图像失败!我不知道为什么。

这是控制台:

[27 17:48:25,515 DEBUG] [http-nio-8080-exec-2] servlet.DispatcherServlet - DispatcherServlet with name 'todo' processing GET request for [/ToDoList/login]
[27 17:48:25,527 DEBUG] [http-nio-8080-exec-2] annotation.RequestMappingHandlerMapping - Looking up handler method for path /login
[27 17:48:25,535 DEBUG] [http-nio-8080-exec-2] annotation.RequestMappingHandlerMapping - Returning handler method [public org.springframework.web.servlet.ModelAndView code.controller.LoginController.showLoginPage()]
[27 17:48:25,535 DEBUG] [http-nio-8080-exec-2] support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'loginController'
[27 17:48:25,538 DEBUG] [http-nio-8080-exec-2] servlet.DispatcherServlet - Last-Modified value for [/ToDoList/login] is: -1
[27 17:48:25,614 DEBUG] [http-nio-8080-exec-2] support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'login'
[27 17:48:25,615 DEBUG] [http-nio-8080-exec-2] servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'login'; URL [/WEB-INF/views/login.jsp]] in DispatcherServlet with name 'todo'
[27 17:48:25,615 DEBUG] [http-nio-8080-exec-2] view.JstlView - Added model object 'firstuser' of type [code.model.FirstUser] to request in view with name 'login'
[27 17:48:25,615 DEBUG] [http-nio-8080-exec-2] view.JstlView - Added model object 'org.springframework.validation.BindingResult.firstuser' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'login'
[27 17:48:25,625 DEBUG] [http-nio-8080-exec-2] view.JstlView - Forwarding to resource [/WEB-INF/views/login.jsp] in InternalResourceView 'login'
[27 17:48:25,799 DEBUG] [http-nio-8080-exec-2] beans.BeanUtils - No property editor [java.lang.StringEditor] found for type java.lang.String according to 'Editor' suffix convention
[27 17:48:25,812 DEBUG] [http-nio-8080-exec-2] servlet.DispatcherServlet - Successfully completed request
[27 17:48:26,232 DEBUG] [http-nio-8080-exec-3] servlet.DispatcherServlet - DispatcherServlet with name 'todo' processing GET request for [/ToDoList/resources/css/login.css]
[27 17:48:26,232 DEBUG] [http-nio-8080-exec-4] servlet.DispatcherServlet - DispatcherServlet with name 'todo' processing GET request for [/ToDoList/resources/js/login.js]
[27 17:48:26,232 DEBUG] [http-nio-8080-exec-5] servlet.DispatcherServlet - DispatcherServlet with name 'todo' processing GET request for [/ToDoList/resources/library/jquery-3.0.0.min.js]
[27 17:48:26,232 DEBUG] [http-nio-8080-exec-3] annotation.RequestMappingHandlerMapping - Looking up handler method for path /resources/css/login.css
[27 17:48:26,233 DEBUG] [http-nio-8080-exec-5] annotation.RequestMappingHandlerMapping - Looking up handler method for path /resources/library/jquery-3.0.0.min.js
[27 17:48:26,232 DEBUG] [http-nio-8080-exec-4] annotation.RequestMappingHandlerMapping - Looking up handler method for path /resources/js/login.js
[27 17:48:26,233 DEBUG] [http-nio-8080-exec-6] servlet.DispatcherServlet - DispatcherServlet with name 'todo' processing GET request for [/ToDoList/resources/library/jquery-ui-1.11.4.custom/jquery-ui.min.js]
[27 17:48:26,234 DEBUG] [http-nio-8080-exec-6] annotation.RequestMappingHandlerMapping - Looking up handler method for path /resources/library/jquery-ui-1.11.4.custom/jquery-ui.min.js
[27 17:48:26,235 DEBUG] [http-nio-8080-exec-5] annotation.RequestMappingHandlerMapping - Did not find handler method for [/resources/library/jquery-3.0.0.min.js]
[27 17:48:26,235 DEBUG] [http-nio-8080-exec-3] annotation.RequestMappingHandlerMapping - Did not find handler method for [/resources/css/login.css]
[27 17:48:26,237 DEBUG] [http-nio-8080-exec-3] handler.SimpleUrlHandlerMapping - Matching patterns for request [/resources/css/login.css] are [/resources/**]
[27 17:48:26,235 DEBUG] [http-nio-8080-exec-4] annotation.RequestMappingHandlerMapping - Did not find handler method for [/resources/js/login.js]
[27 17:48:26,237 DEBUG] [http-nio-8080-exec-6] annotation.RequestMappingHandlerMapping - Did not find handler method for [/resources/library/jquery-ui-1.11.4.custom/jquery-ui.min.js]
[27 17:48:26,243 DEBUG] [http-nio-8080-exec-4] handler.SimpleUrlHandlerMapping - Matching patterns for request [/resources/js/login.js] are [/resources/**]
[27 17:48:26,236 DEBUG] [http-nio-8080-exec-5] handler.SimpleUrlHandlerMapping - Matching patterns for request [/resources/library/jquery-3.0.0.min.js] are [/resources/**]
[27 17:48:26,243 DEBUG] [http-nio-8080-exec-6] handler.SimpleUrlHandlerMapping - Matching patterns for request [/resources/library/jquery-ui-1.11.4.custom/jquery-ui.min.js] are [/resources/**]
[27 17:48:26,247 DEBUG] [http-nio-8080-exec-4] handler.SimpleUrlHandlerMapping - URI Template variables for request [/resources/js/login.js] are {}
[27 17:48:26,247 DEBUG] [http-nio-8080-exec-3] handler.SimpleUrlHandlerMapping - URI Template variables for request [/resources/css/login.css] are {}
[27 17:48:26,247 DEBUG] [http-nio-8080-exec-6] handler.SimpleUrlHandlerMapping - URI Template variables for request [/resources/library/jquery-ui-1.11.4.custom/jquery-ui.min.js] are {}
[27 17:48:26,247 DEBUG] [http-nio-8080-exec-5] handler.SimpleUrlHandlerMapping - URI Template variables for request [/resources/library/jquery-3.0.0.min.js] are {}
[27 17:48:26,253 DEBUG] [http-nio-8080-exec-5] handler.SimpleUrlHandlerMapping - Mapping [/resources/library/jquery-3.0.0.min.js] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/resources/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@6f2e3841]]] and 1 interceptor
[27 17:48:26,253 DEBUG] [http-nio-8080-exec-4] handler.SimpleUrlHandlerMapping - Mapping [/resources/js/login.js] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/resources/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@6f2e3841]]] and 1 interceptor
[27 17:48:26,253 DEBUG] [http-nio-8080-exec-3] handler.SimpleUrlHandlerMapping - Mapping [/resources/css/login.css] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/resources/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@6f2e3841]]] and 1 interceptor
[27 17:48:26,253 DEBUG] [http-nio-8080-exec-6] handler.SimpleUrlHandlerMapping - Mapping [/resources/library/jquery-ui-1.11.4.custom/jquery-ui.min.js] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/resources/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@6f2e3841]]] and 1 interceptor
[27 17:48:26,255 DEBUG] [http-nio-8080-exec-5] servlet.DispatcherServlet - Last-Modified value for [/ToDoList/resources/library/jquery-3.0.0.min.js] is: -1
[27 17:48:26,255 DEBUG] [http-nio-8080-exec-4] servlet.DispatcherServlet - Last-Modified value for [/ToDoList/resources/js/login.js] is: -1
[27 17:48:26,255 DEBUG] [http-nio-8080-exec-3] servlet.DispatcherServlet - Last-Modified value for [/ToDoList/resources/css/login.css] is: -1
[27 17:48:26,255 DEBUG] [http-nio-8080-exec-6] servlet.DispatcherServlet - Last-Modified value for [/ToDoList/resources/library/jquery-ui-1.11.4.custom/jquery-ui.min.js] is: -1
[27 17:48:26,263 DEBUG] [http-nio-8080-exec-6] servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'todo': assuming HandlerAdapter completed request handling
[27 17:48:26,264 DEBUG] [http-nio-8080-exec-6] servlet.DispatcherServlet - Successfully completed request
[27 17:48:26,265 DEBUG] [http-nio-8080-exec-4] servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'todo': assuming HandlerAdapter completed request handling
[27 17:48:26,266 DEBUG] [http-nio-8080-exec-4] servlet.DispatcherServlet - Successfully completed request
[27 17:48:26,270 DEBUG] [http-nio-8080-exec-5] servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'todo': assuming HandlerAdapter completed request handling
[27 17:48:26,270 DEBUG] [http-nio-8080-exec-5] servlet.DispatcherServlet - Successfully completed request
[27 17:48:26,274 DEBUG] [http-nio-8080-exec-3] servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'todo': assuming HandlerAdapter completed request handling
[27 17:48:26,274 DEBUG] [http-nio-8080-exec-3] servlet.DispatcherServlet - Successfully completed request

最佳答案

在您的 jsp 中导入 c 标签:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

像下面这样更改 jsp 上的代码:

<img src="<c:url value="/resources/img/calendar.png"/>"/>

关于html - 为什么图像的 URL 在 css 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38063705/

相关文章:

html - 使图片位于文章的左侧

javascript - HTML5 视频无缝循环

php - Magento - 图像和可配置色板的恒定产品尺寸

css - 使用#ID 作为文体钩子(Hook)的充分理由

java - 由于图像加载,GUI 不断崩溃?

PHP - ImageCreate() 错误

html - 如何使尺寸在所有屏幕上看起来都一样

jquery - 显示 div 可以容纳什么

JQuery 动画 "width: toggle"导致间歇性闪烁

jquery - 如何jquery在右键单击时将类添加到div