【发布时间】:2016-10-30 01:01:43
【问题描述】:
我花了一整天的时间在这个问题上,并在谷歌上搜索了很多答案。
我的 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
【问题讨论】:
-
你能发布控制台错误吗?
-
有时,如果扩展名是 JPG 或 PNG,如果您编写 jpg 或 png,则会遇到问题。仔细检查扩展,如果它不能解决问题,然后像安德鲁所说的那样发布控制台错误
-
可能值得检查一下,您可以通过键入图像的完整 URL 直接在浏览器中加载图像。如果未加载,请检查图像文件和包含目录的文件权限和所有权。
-
@JohnSheridan 我可以使用浏览器上的完整 URL 成功加载图像。
-
@AndrewRibeiro 我更新了它。但是我在控制台信息中没有看到任何关于图像的错误警告。