Html 图像未显示在 JSP 页面上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12794121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image not displayed on JSP page
提问by user1701556
I have a simple JSP page that has one image that is part of the JSP page but that image is not displayed when I view the webpage. I can view the webpage but it does not include the image.
我有一个简单的 JSP 页面,其中有一个图像是 JSP 页面的一部分,但是当我查看网页时没有显示该图像。我可以查看网页,但它不包含图像。
I have my img folder located in several places because I am trying to fix this problem. Inside the img folder is where I have img.jpg image that should be displayed inside JSP page.
我的 img 文件夹位于多个位置,因为我正在尝试解决此问题。在 img 文件夹中,我有 img.jpg 图像,应该显示在 JSP 页面中。
war/img/img.jpg
war/img/img.jpg
war/WEB-INF/jsp/img/img.jpg
war/WEB-INF/jsp/img/img.jpg
I am also not able to view img.jpg directly from my web server. For example: //localhost:8080/register/img/img.jpg
I get HTTP Status 404 -
我也无法直接从我的 Web 服务器查看 img.jpg。例如://localhost:8080/register/img/img.jpg
我得到 HTTP 状态 404 -
Here is my web.xml file
这是我的 web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringExampleSanjeev</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>/img/*</url-pattern>
</servlet-mapping>
<!-- Enable these types of content(files) to be viewed from webpage -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
</web-app>
Here is snippet of my JSP page
这是我的 JSP 页面的片段
<img border="0" src="/img/img.jpg" alt="header banner" width="304" height="228">
<img src="<c:url value="/img/img.jpg" />" alt="TestDisplay"/>
<img src="<c:url value='/img/img.jpg'/>" />
<img alt="OS usage" src="/img/img.jpg">
<img border="0" src="/WEB-INF/jsp/img/img.jpg" alt="header banner" width="304" height="228">
<img border="0" src="/register/WEB-INF/jsp/img/img.jpg" alt="header banner" width="304" height="228">
<img src="img.jpg">
<img src="/img/img.jpg">
<img src="/jsp/img/img.jpg">
<img src="/register/jsp/img/img.jpg">
<img src="/WEB-INF/jsp/img/img.jpg">
<img src="/register/WEB-INF/jsp/img/img.jpg">
I am not sure what I am doing wrong. Putting an simple image on JSP page should not be this hard.
我不确定我做错了什么。在 JSP 页面上放置一个简单的图像应该不难。
回答by R Shah
Please try with this.
请试试这个。
images folder parallel to WEB-INF. (images folder should NOT be inside WEB-INF).
与 WEB-INF 平行的图像文件夹。(图像文件夹不应在 WEB-INF 内)。
In JSP,
在 JSP 中,
<img src="../images/img.jpg" alt=""/>
回答by ayoub
I had the same problem and resolved it by placing images in the server's directory structure as opposed to the portlet's. I'm using apache-tomcat and have it downloaded under:
我遇到了同样的问题,并通过将图像放置在服务器的目录结构而不是 portlet 的目录结构中来解决它。我正在使用 apache-tomcat 并将其下载到:
C:\apache-tomcat-5.5.25\
C:\apache-tomcat-5.5.25\
In it there is a folder called "webapps" and I created a subfolder for my images there
其中有一个名为“webapps”的文件夹,我在那里为我的图像创建了一个子文件夹
C:\apache-tomcat-5.5.25\webapps\portal_content\img
C:\apache-tomcat-5.5.25\webapps\portal_content\img
From the portlet JSP (i.e. view.jsp) I refrence the image as such:
从 portlet JSP(即 view.jsp)中,我引用了这样的图像:
<"img src="/portal_content/img/my_image.gif">
<"img src="/portal_content/img/my_image.gif">
Hope this helps.
希望这可以帮助。