Javascript JPG 图像未出现在动态 Web 项目的 JSP 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10021990/
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
JPG Image not appearing in JSP of dynamic web project
提问by somnathchakrabarti
I am trying to use an image in the below index.jsp file in a dynamic web project that I created in Eclipse Indigo IDE. I have added the image under WEB-INF/images/pict1.jpg under WebContent of my project folder. But when I am running in browser, the image is not visible. The text and submit buttons are coming as expected. Am I missing anything? Any idea about how to make the image appear?
我正在尝试在我在 Eclipse Indigo IDE 中创建的动态 Web 项目中使用以下 index.jsp 文件中的图像。我已经在我的项目文件夹的 WebContent 下的 WEB-INF/images/pict1.jpg 下添加了图像。但是当我在浏览器中运行时,图像不可见。文本和提交按钮按预期出现。我错过了什么吗?关于如何使图像出现的任何想法?
Any help will be appreciated. Below is the code.
任何帮助将不胜感激。下面是代码。
Thanks,
谢谢,
Somnath
索姆纳特
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<c:import url="/WEB-INF/javascript/index.js" />
<title>My Page</title>
</head>
<body>
<img alt="picture1" src="images/pict1.jpg">
<table border="0">
<tr><td valign="top">
<h1>Continue</h1></td><td><input type="submit"></td></tr>
<tr><td valign="top">
<h1>Continue</h1></td><td><input type="submit"></td></tr>
</table>
</body>
</html>
回答by Steve H.
The image 'src' attribute is relative to the context root of your web application. Images don't go in WEB-INF. Move the 'images' folder to the "WebContent" folder of your project.
图像 'src' 属性与 Web 应用程序的上下文根相关。图像不会进入 WEB-INF。将“images”文件夹移动到项目的“WebContent”文件夹中。
WEB-INF is usually reserved for metadata about your project that the container uses. Your application jsps and other resources are usually in folders relative to the WebContent folder.
WEB-INF 通常保留用于容器使用的项目的元数据。您的应用程序 jsps 和其他资源通常位于相对于 WebContent 文件夹的文件夹中。
回答by Carlos Gavidia-Calderon
According to thisdocument from Oracle:
根据Oracle 的这份文件:
The WEB-INF directory is not part of the public document tree of the application. No file contained in the WEB-INF directory can be served directly to a client by the container.
WEB-INF 目录不是应用程序公共文档树的一部分。容器不能直接向客户端提供 WEB-INF 目录中包含的任何文件。
Files contained in WEB-INF
folder are not accessible by URL. If you're using Eclipse, put pict1.jpg
image in WebContent/images
so your JSP point to a valid location.
文件WEB-INF
夹中包含的文件无法通过 URL 访问。如果您使用的是 Eclipse,请将pict1.jpg
图像放入,WebContent/images
以便您的 JSP 指向有效位置。