简体   繁体   English

Java Web应用程序。 定位图像时遇到的问题

[英]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.

相关问题 Java Web应用程序。 春季启动。 找到图像 - Java Web Application. Spring Boot. Locating Images 试图在 Java Web 应用程序中实现 SQL 漏洞。 Java + MySQL - Trying to implement an SQL vulnerability in a java web application. Java + MySQL 部署java Web应用程序。 一些listenerStart错误 - Deploying the java web application. some listenerStart error 部署Java应用程序。 怎么样? - Deploying a Java application. How? 将Azure AD集成到Java Web应用程序中。 如何破解Azure AD - Integrating Azure AD into a Java web application. How to break Azure AD 我想构建一个Java Web应用程序。 最快的开始是什么? - I would like to build a Java Web Application. What's the quickest start? 我的 java/selenium 项目突然出现错误,但 web 应用程序中的元素仍然存在。 有谁知道如何解决这个问题? - My java/selenium project suddenly giving errors but the elements are still there on the web application. Anyone know how to solve this? 我正在使用 selenium 和 java 来自动化基于 Web 的应用程序。 我无法与浏览按钮交互 - Am using selenium with java for automating web based application. I couldnt interact with browse button Web应用程序中一些字符串的翻译。 - Translation of a few strings in a web application. Java 中 Spring Boot Web 应用程序的问题? - Problems with Spring Boot web application in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM