Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring mvc+;maven:无法访问图像_Java_Spring_Maven - Fatal编程技术网

Java Spring mvc+;maven:无法访问图像

Java Spring mvc+;maven:无法访问图像,java,spring,maven,Java,Spring,Maven,我使用的是webapp的标准maven原型,所以我将images文件夹放在src/main/resources目录下,在war包中它放在WEB-INF/classes/directory下 我的项目树如下图所示 所以images目录在类路径中,我通过getResource加载它 final String path ="/resources/images/disegno.svg"; URL imagePath = this.getClass().getClassLoader().getResour

我使用的是webapp的标准maven原型,所以我将images文件夹放在src/main/resources目录下,在war包中它放在WEB-INF/classes/directory下

我的项目树如下图所示

所以images目录在类路径中,我通过getResource加载它

final String path ="/resources/images/disegno.svg";
URL imagePath = this.getClass().getClassLoader().getResource(path);
但我明白了

Could not complete request
java.lang.NullPointerException
在声明路径时 同样在.jsp中,我尝试通过以下方式访问图像:

<img src="<%=request.getContextPath() %>/WEB-INF/classes/images/disegno.svg"/>

我无法理解maven在这种情况下是如何工作的,也找不到完整的指南,因为所有地方看起来都很简单,所以我不知道我在犯什么错误…

你犯了3个错误

第一:
ClassLoader.getResource()
需要一个不以
/
开头的路径

第二:源项目中
src/main/resources
下的内容位于类路径的根。正如您的WEB-INF/类截图所示,没有
资源
包。所以你的道路应该是

this.getClass().getClassLoader().getResource("images/disegno.svg")
第三:从设计上讲,所有的undex WEB-INF都不能从浏览器访问。WEB-INF之外的所有内容都是。因此,如果您希望浏览器能够下载您的图像,它应该位于
src/main/webapp
文件夹中,而不是
src/main/resources
中。当然,一旦它出现了,你就不能再用类加载器加载它了,因为它不再在类路径中了。如果您需要加载它以使其直接可供浏览器使用,那么请使用
ServletContext.getResource()

是否可能重复?
this.getClass().getClassLoader().getResource("images/disegno.svg")