[英]Java Web Application. Problems in locating images
I'm making a java web app, and I want it to display an image. 我正在制作一个Java Web应用程序,并且希望它显示图像。 However, it doesn´t find my image. 但是,找不到我的图像。
I've made a folder in /src/main/resources/images
我在/src/main/resources/images
了一个文件夹
Also, in the .jsp
file, I´ve tried with the following sentences. 另外,在.jsp
文件中,我尝试使用以下句子。
<img src="/src/main/resources/images/Head.png"> </img>
<img src="< c:url value='/src/main/resources/images/Head.png'/>"> </img>
Is there anything bad I'm doing? 我有什么不好的吗?
Thanks 谢谢
Edit: 编辑:
The path of mi .jsp
file is /src/main/webapp/WEB-INF/jsp/welcome.jsp
mi .jsp
文件的路径是/src/main/webapp/WEB-INF/jsp/welcome.jsp
You can find the web app code in https://github.com/Santi-7/hello 您可以在https://github.com/Santi-7/hello中找到该Web应用程序代码
The structure of a .war
file is as follows: .war
文件的结构如下:
/
/WEB-INF
/lib
/classes
/META-INF
Now, your application has the following structure (I assume, given the folder structure, you are using Maven) 现在,您的应用程序具有以下结构(假定文件夹结构,我假设您正在使用Maven)
/
/src
/main
/java
/resources
/webapp
Now, the Maven war plugin will copy everything in the classpath to /WEB-INF/classes
during compilation - this is /src/main/java
and /src/main/resources
by default. 现在,Maven war插件将在编译期间将类路径中的所有内容复制到/WEB-INF/classes
默认情况下为/src/main/java
和/src/main/resources
。
The crux of the matter is that nothing under /WEB-INF
or /META-INF
can be accessed by requests - this is for security as otherwise someone could simply download /WEB-INF/web.xml
for example. 问题的症结在于/WEB-INF
或/META-INF
下的任何内容都不能被请求访问 -这是为了安全起见,否则有人可以简单地下载/WEB-INF/web.xml
。
So, in order to add a resource that is accessible by a browser, you need to place it into /src/main/webapp
- this will become the root of the application. 因此,为了添加浏览器可访问的资源,您需要将其放入/src/main/webapp
这将成为应用程序的根。
So if you place Head.png
into /src/main/webapp/images
then in the JSP you would use: 因此,如果将Head.png
放在/src/main/webapp/images
那么在JSP中,您将使用:
<c:url value='/images/Head.png'/>
In short, you need to read up on how the directory structure of a .war
works and how that relates to your code. 简而言之,您需要阅读.war
的目录结构如何工作以及与代码的关系。
Your app is a Spring Boot app . 您的应用程序是Spring Boot应用程序 。 I think that you can also try to use the facilities provided by Spring Boot for serving static content. 我认为您也可以尝试使用Spring Boot提供的功能来提供静态内容。 Anyway, you are doing it right now because you are using webjars for css and js libs!!! 无论如何,您现在正在执行此操作,因为您正在将Webjars用于CSS和JS库! Be consistent with the tech that you are using. 与您使用的技术保持一致。
The path to the image must be relative to the path to the .jsp file. 图像的路径必须相对于.jsp文件的路径。
Because the path to your image is: /src/main/resources/images/Head.png
, and the path to your jsp file is: /src/main/webapp/WEB-INF/jsp/welcome.jsp
, in your image tag you need to write: 因为图像的路径为/src/main/resources/images/Head.png
,而jsp文件的路径为: /src/main/webapp/WEB-INF/jsp/welcome.jsp
,您需要编写的标签:
<img src="../../../resources/images/Head.png" />
The ../../../
is for getting out from the jsp
folder to the main
folder, and the resources/images/Head.png
is the path from the main
folder to the image. ../../../
用于从jsp
文件夹移出到main
文件夹,而resources/images/Head.png
是从main
文件夹到图像的路径。
Thanks everybody, i could resolve my problem. 谢谢大家,我可以解决我的问题。
Changes I made: 我所做的更改:
So, in order to add a resource that is accessible by a browser, you need to place it into /src/main/webapp - this will become the root of the application. 因此,为了添加浏览器可访问的资源,您需要将其放入/ src / main / webapp-这将成为应用程序的根。
Now, my images are in /src/main/webapp/images
. 现在,我的图像在/src/main/webapp/images
。
The path to the image must be relative to the path to the .jsp file. 图像的路径必须相对于.jsp文件的路径。
Now, the sentence of my .jsp
file is 现在,我的.jsp
文件的句子是
<img src="images/Head.png" />
Edit [1]: 编辑[1]:
¡ I made a mistake. 我犯了一个错误。 The path to the image is relative to the /webapp classpath ! 图像的路径是相对于/ webapp类路径的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.