图像不在JSP中显示

时间:2018-06-07 15:06:17

标签: java image jsp

我创建了一个动态Web项目,以便通过Servlet显示JSP(我不能像Spring那样使用任何框架......)。默认情况下,我的构建类被放置在Build / classes中,所以在我将输出文件夹更改为WebContent / WEB-INF / classes之后,我的应用程序运行正常。后记我将项目转换为Maven项目。 (我不记得上次使用Servlets / 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服务器

如果你能找到解决方案(而且解释也会很好),我将非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

在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的基本结构,它将与这种结构产生战争

  |-- 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