简体   繁体   English

在tomcat中部署war文件后,无法在html中访问图像的相对路径

[英]Relative path is not accessing in html for an image after war file deployment in tomcat

Test project is deployed in Tomcat 7.0. 测试项目已部署在Tomcat 7.0中。 Below are the directory structure of deployment project. 以下是部署项目的目录结构。 We are facing the relative path issue in this Test project for an image. 我们在此测试项目中面临图像的相对路径问题。

 /webapps
   |--/Test
        |--/images
              |-- logo.jpg
        |--/META-INF
              |-- MANIFEST.MF
        |--/WEB-INF
              |-- /classes
              |-- /html
                    |--/index.html
              |--/lib
              |-- web.xml

below is my index.html page and web.xml file 以下是我的index.html页面和web.xml文件

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>
<body>
    <img src="../../images/logo.jpg"/>
</body>
</html>

<welcome-file-list>
    <welcome-file>/WEB-INF/html/index.html</welcome-file>
</welcome-file-list>

When we try to access http://localhost:8080/Test/ then logo.jpg is not loaded. 当我们尝试访问http:// localhost:8080 / Test /时,不会加载logo.jpg。 Relative path is not accessing in index.html after deployment in tomcat 7. 在tomcat 7中部署后,相对路径无法在index.html中访问。

尝试

<img src="images/logo.jpg"/>

Instead of giving a related path, best practice is to use absolute dynamic path starting from your context. 最佳实践是从上下文开始使用绝对动态路径,而不是给出相关路径。

As below: 如下:

<img src="<%=request.getContextPath() %>/images/logo.jpg" />

Or better in jstl using ${pageContext.servletContext.contextPath} 或者在jstl中使用${pageContext.servletContext.contextPath}更好

<img src="${pageContext.servletContext.contextPath}/images/logo.jpg" />

It will always help you irrespective of your directory structure. 无论您的目录结构如何,它将始终为您提供帮助。

you can use: 您可以使用:

${pageContext.request.contextPath}

this will return your current context path and hence you can access anywhere from here. 这将返回您当前的上下文路径,因此您可以从此处访问任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM