我正在使用 spring 工具套件。我已将图像文件放在文件夹 WebContent/WEB-INF/resources
<img src="WebContent/WEB-INF/resources/team_pic1.jpg" alt="Mountain View" style="width:304px;height:228px;">
这是我的 servlet.xml 代码
<mvc:resources mapping="/resources/" location="/resources/" />
我收到错误为
Failed to load resource: the server responded with a status of 404 (Not Found) at http://localhost:8080/SpringMVCTest/WebContent/WEB-INF/resources/team_pic1.jpg
这是我的目录结构
请让我知道我哪里出了问题
这是我通过浏览器运行时遇到的错误
最佳答案
尝试一下:
<c:url var="imgUrl" value="/resources/team_pic1.jpg" />
<img src="${imgUrl}" alt="Mountain View" style="width:304px;height:228px;">
这会构建正确的 URL 来访问资源处理程序。
您还必须更正资源映射:
- 如果你想传递“子资源”,那么你需要添加
/**
. - 根据您的资源文件所在的位置,您还需要更正位置属性:我预计
/WEB-INF/resources/
是你的 war 资源所在的文件夹!
所以我认为这就是您需要的资源配置:
<resources mapping="/resources/**" location="/WEB-INF/resources/" />
关于spring - 我无法在 spring mvc 项目中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31489108/