![](/img/trans.png)
[英]Maven project - load file from outside of resources folder (project root)
[英]Unable to load images from resources in spring maven project
我添加了一個包含單個圖像的簡單頁面,該頁面位於“ / resources / images /”文件夾中,但是在服務器上運行時,圖像不會加載到該頁面上。 關注了許多博客,但無法解決此問題。 控制器映射如下:
@RequestMapping(value = "/image", method = RequestMethod.GET)
public ModelAndView imagepage() {
ModelAndView img = new ModelAndView("image_check");
return img;
}
image_check.jsp文件如下:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<img src="/resources/ABC_bank.png" alt="Smiley face" height="42" width="42">
</html>
project-servlet.xml如下:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/images/" />
圖像位於“ / webapp / resources / images /”文件夾中。 頁面打開,但是圖像不加載。
在project-servlet.xml中進行以下更改。
<mvc:resources mapping="/resources/**" location="/resources/" />
然后參考圖片
<img src="/resources/images/logo.jpg" />
將此作為參考。
可以通過在jsp文件中添加以下標記來訪問靜態引用
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
以下行將/resources/
映射到/resources/images/
文件夾:
<mvc:resources mapping="/resources/**" location="/resources/images/" />
因此,image_check.jsp的最終修改代碼如下:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<img src="<c:url value="/resources/ABC_bank.png"/>">
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.