简体   繁体   English

如何在Spring MVC中从我的resources / images文件夹加载图像?

[英]How can I load an image from my resources/images folder in spring mvc?

I am trying to load 2 images from my resources/images folder and display them on my index.jsp: 我正在尝试从我的resources / images文件夹加载2张图像,并将它们显示在index.jsp上:

<img src="<c:url value='/resources/images/harry_potter.jpg'/>"/>
<img alt="Image" src="${pageContext.request.contextPath}/resources/images/harry_potter.jpg">

<img src="<c:url value='/resources/images/hp.jpg'/>"/>
<img alt="Image" src="${pageContext.request.contextPath}/resources/images/hp.jpg">

When I load the page images don't appear, instead there are image icons. 当我加载页面时,图像不会出现,而是有图像图标。 As you've seen in my index.jsp, I've tried 2 different methods for loading an image 如您在index.jsp中所见,我尝试了2种不同的方法来加载图像

I have an application.xml page and a dispatcher-servlet page.xml 我有一个application.xml页面和一个dispatcher-servlet page.xml

In each file I have tried putting this under the context:component-scan 在每个文件中,我都尝试将其放在上下文中:component-scan

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

but I still do not see my images on the index page. 但我仍然无法在索引页面上看到我的图像。 Does anyone know why this isn't showing 有谁知道为什么没有显示

My File Structure for reference: 我的文件结构供参考:
File Image View 文件图像视图
文件图像视图

In my web.xml for reference: 在我的web.xml中供参考:

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

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

Console for reference 控制台供参考

Console Image View 控制台映像视图
控制台映像视图

Try this please, may work;) 请尝试这个,可能会起作用;)

Change 更改

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

to

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

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

相关问题 如何使用Spring MVC将图像上传到webapp / resources / images /目录? - How can i upload image to webapp/resources/images/ directory using spring mvc? Java:如何从资源文件夹中加载.properties? - Java: how can I load .properties from resources folder? 如何使用spring mvc将图像上传到webapp / resources / images目录? - How to upload an image to webapp/resources/images directory using spring mvc? Spring 安全性:我可以加载 static 资源但不能加载图像 - Spring Security:I can load static resources but not images 如何将资源文件夹中的图像加载到android listview - how to load images from resources folder into android listview 如何从资源构建路径加载ResourceBundle? - How can I load a ResourceBundle from my resources build path? 如何告诉spring从外部文件夹加载资源? - how to tell spring to load resources from external folder? JavaFX-MVC-如何从另一个包加载图像 - JavaFX - MVC - How can I load images from another package 如何使Spring Boot从资源中看到我的图像 - How to make Spring Boot see my images from resources 我如何将我的视图文件(.html)资源从“webapp”设置到 SpringMVC 项目中的“resources”文件夹 - How I can set my Views files(.html) resource from "webapp" to "resources" folder in SpringMVC project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM