gpt4 book ai didi

java - JSP 中图像不显示

转载 作者:行者123 更新时间:2023-12-02 11:08:37 24 4
gpt4 key购买 nike

我创建了一个动态 Web 项目,以便通过 Servlet 显示 JSP(我不能使用任何像 Spring 这样的框架...)。默认情况下,我构建的类放置在 Build/classes 中,因此在我将输出文件夹更改为 WebContent/WEB-INF/classes 后,我的应用程序运行良好。后记 我将项目转换为 Maven 项目。 (我不记得上次使用 Servlet/JSP 时需要更改输出文件夹,那是 3 年前的事了!)。

无论如何,现在我无法在页面中显示图像,我将图像放置在 WebContent/images/logo.jpg 和/images/logo.jpg 中(这两个位置都不起作用!)

这是我添加到 JSP 中以显示图像的内容

<img alt="1" src="/images/logo.jpg" width="88"> 
<img alt="2" src="images/logo.jpg" width="88">
<img alt="3" src="<c:url value='/images/logo.jpg'/>" width="88">
<img alt="4" src="<c:url value='images/logo.jpg'/>" width="88">
<img alt="5" src="/<c:url value='/images/logo.jpg'/>" width="88">
<img alt="6" src="/<c:url value='images/logo.jpg'/>" width="88">
<img alt="7" src="<% request.getContextPath(); %>/images/logo.jpg" width="88">
<img alt="8" src="<% request.getContextPath(); %>images/logo.jpg" width="88">
<img alt="9" src="/<% request.getContextPath(); %>/images/logo.jpg" width="88">
<img alt="A" src="/<% request.getContextPath(); %>images/logo.jpg" width="88">
<img alt="B" src="/WebContent/images/logo.jpg" width="88">
<img alt="C" src="WebContent/images/logo.jpg" width="88">
<img alt="D" src="<c:url value='/WebContent/images/logo.jpg'/>" width="88">
<img alt="E" src="<c:url value='WebContent/images/logo.jpg'/>" width="88">
<img alt="F" src="/<c:url value='/WebContent/images/logo.jpg'/>" width="88">
<img alt="G" src="/<c:url value='WebContent/images/logo.jpg'/>" width="88">
<img alt="H" src="<% request.getContextPath(); %>/WebContent/images/logo.jpg" width="88">
<img alt="I" src="<% request.getContextPath(); %>WebContent/images/logo.jpg" width="88">
<img alt="J" src="/<% request.getContextPath(); %>/WebContent/images/logo.jpg" width="88">
<img alt="K" src="<% request.getContextPath(); %>WebContent/images/logo.jpg" width="88">
<img alt="L" src="${pageContext.request.contextPath}/WebContent/images/logo.jpg" width="88">
<img alt="M" src="${pageContext.request.contextPath}WebContent/images/logo.jpg" width="88">
<img alt="N" src="/${pageContext.request.contextPath}/WebContent/images/logo.jpg" width="88">
<img alt="O" src="/${pageContext.request.contextPath}WebContent/images/logo.jpg" width="88">
<img alt="P" src="${pageContext.request.contextPath}/images/logo.jpg" width="88">
<img alt="Q" src="${pageContext.request.contextPath}images/logo.jpg" width="88">
<img alt="R" src="/${pageContext.request.contextPath}/images/logo.jpg" width="88">
<img alt="S" src="/${pageContext.request.contextPath}images/logo.jpg" width="88">

我还将这一行添加到 jsp 中以调试 contextPath

<c:url value='images/logo.jpg'/><br /><br /> <!-- shows: images/logo.jpg -->
<% request.getContextPath(); %> <br /> <!-- shows nothing! in the JSP but shows /GraphGenerator from the servlet (using sysout)-->
${pageContext.request.contextPath} <br /> <!-- shows: /GraphsGenerator (name of the project as displayed in the url http:localhost:8080/GraphsGenerator/ -->

这是项目的结构:

enter image description here

我正在使用:

  • Java 8。
  • Eclipse IDE。
  • 动态网络模块 v3.1。
  • Tomcat v9.0 服务器

如果您能找到解决方案(并且给出解释也很好),我将非常感激。谢谢!

最佳答案

在 scr 文件夹中添加文件夹资源并在其中添加图像

 |-- pom.xml
`-- src
`-- main
|-- java
| `-- com
| `-- example
| `-- projects
| `-- SampleAction.java
|-- resources
| `-- images
| `-- sampleimage.jpg // Add your image here
`-- webapp
|-- WEB-INF
| `-- web.xml
|-- index.jsp
`-- jsp
`-- websource.jsp

这是maven的基本结构,它会与这个结构产生 war

  |-- META-INF
| |-- MANIFEST.MF
| `-- maven
| `-- com.example.projects
| `-- documentedproject
| |-- pom.properties
| `-- pom.xml
|-- WEB-INF
| |-- classes
| | |-- com
| | | `-- example
| | | `-- projects
| | | `-- SampleAction.class
| | `-- images
| | `-- sampleimage.jpg
| `-- web.xml
|-- index.jsp
`-- jsp
`-- websource.jsp

来自Maven Documentation

关于java - JSP 中图像不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50744583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com