< img>在JSP中不起作用 [英] <img > is not working in JSP

查看:81
本文介绍了< img>在JSP中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我给出我的JSP文件图像的相对路径时,我正在使用eclipse.但是当我使用图像的绝对路径时,它只能在Eclipse的内部浏览器中使用,但在其他浏览器Firfox,IE等上仍然无法使用.

I am using eclipse when I give relative path from my JSP file image does not appear. but when I use absolute path to my image then it work only in Eclipse' Internal browser but still does not works on other browsers Firfox, IE etc.

推荐答案

显然,相对路径是错误的.

Apparently the relative path was plain wrong.

您需要认识到<img src>应该引用公共URL,而不是Web服务器的本地磁盘文件系统.需要加载图像的是Web浏览器,而不是Web服务器本身.如果此公共URL是相对的,则相对于当前请求URL(您在浏览器的地址栏中看到的URL)进行解析.相对于本地磁盘文件系统中JSP的位置,它尚未解决.

You need to realize that the <img src> should refer to a public URL, not to the local disk file system of the webserver. It's namely the webbrowser who needs to load the image, not the webserver itself. If this public URL is relative, then it is resolved relative to the current request URL, the one which you see in the browser's address bar. It's not resolved relative to the location of the JSP in the local disk file system.

想象一下,您正在此URL上打开JSP页面

Imagine that you're opening the JSP page on this URL

http://localhost:8080/contextname/some.jsp

并且该图片以原始格式可以在此URL上访问

and that the image is in its raw form accessible on this URL

http://localhost:8080/contextname/images/some.png

然后需要在JSP中按如下方式引用图像

then the image needs to be referenced in the JSP as follows

<img src="images/some.png" />

但是,如果通过此URL打开JSP

But if the JSP is openedby this URL

http://localhost:8080/contextname/somefolder/some.jsp

然后需要在JSP中按如下方式引用图像

then the image needs to be referenced in the JSP as follows

<img src="../images/some.png" />

为避免每次都摆弄相对路径,您也可以只使用相对于域的URL.您可以通过在图片网址前添加上下文路径来实现此目的:

To avoid fiddling with relative paths everytime, you can also just use a domain-relative URL. You can do this by prepending the image URL with the context path:

<img src="${pageContext.request.contextPath}/images/some.png" />

这将最终以生成的HTML如下所示(在浏览器中右键单击JSP,查看源代码)

this will end up in the generated HTML as follows (rightclick JSP in browser, View Source)

<img src="/contextname/images/some.png" />

另请参见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆