如何从spring mvc中的resources / images文件夹中加载图像?

时间:2022-01-19 02:29:15

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中看到的那样,我尝试了两种不同的加载图像的方法

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
如何从spring mvc中的resources / images文件夹中加载图像?

我的文件结构供参考:文件图像视图

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
如何从spring mvc中的resources / images文件夹中加载图像?

控制台图像视图

1 个解决方案

#1


0  

Try this please, may work;)

试试这个,可以工作;)

Change

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

to

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

#1


0  

Try this please, may work;)

试试这个,可以工作;)

Change

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

to

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