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

时间:2016-04-29 01:29:50

标签: java spring jsp spring-mvc

我正在尝试从我的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">

当我加载页面时,图像不会出现,而是有图像图标。正如您在我的index.jsp中看到的那样,我尝试了两种不同的方法来加载图像

我有一个application.xml页面和一个dispatcher-servlet page.xml

在每个文件中,我都尝试将其放在上下文中:component-scan

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

但我仍然没有在索引页面上看到我的图片。有谁知道为什么这不显示

我的文件结构供参考:
文件图片视图
File Image View

在我的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 Image View

1 个答案:

答案 0 :(得分:0)

请试试这个,可以工作;)

更改

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

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