简体   繁体   English

如何从Web应用程序的根目录引用Java Web应用程序中的任何图像?

[英]How do i refer to any image in my java web application from root of my web application?

I have a folder named Common/Images/Photounavailable.jpg . 我有一个名为Common/Images/Photounavailable.jpg的文件夹。 Is this right way of refering to a image from root? 这是从根引用图像的正确方法吗? I want to refer this image from any page in any folder in my web application. 我想从Web应用程序中任何文件夹中的任何页面引用此图像。

private String getPhotoUnavailablePath = "/Common/Images/PhotoNotAvailable.jpg";
    private String getPhotoUnavailablePath(){
        return photoUnavailablePath;
    }

And from my JSF page i write something like :- 在我的JSF页面上,我写了类似:-

<h:outputLink id ="blogPhoto" value="#{BlogBean.photoUnavailablePath}" >
  <h:graphicImage value="#{BlogBean.photoUnavailablePath}" style="width: 180px;height: 180px;"/>
</h:outputLink>

I get the image to see in h:graphicImage. 我得到要在h:graphicImage中看到的图像。 But when i click on it, i get 404 error. 但是,当我单击它时,出现404错误。 OutputLink doesn't get proper link to image. OutputLink没有获得正确的图像链接。 How is it possible? 这怎么可能? That h:graphicImage gets proper link and h:outputLink doesn't? 该h:graphicImage获得正确的链接,而h:outputLink却没有?

Further, this code works perfectly :- 此外,此代码可以完美运行:-

<h:outputLink id ="blogPhoto" value="..#{BlogBean.photoUnavailablePath}" >
  <h:graphicImage value="#{BlogBean.photoUnavailablePath}" style="width: 180px;height: 180px;"/>
</h:outputLink>

Just by putting .. it works! 只需通过..就可以了! Can anybody explain me what is happening and how do I solve this? 谁能解释我发生了什么事,我该如何解决?

<h:graphicImage> uses context-relative paths. <h:graphicImage>使用上下文相对路径。
<h:outputLink> uses absolute paths. <h:outputLink>使用绝对路径。 That's why you can't use the / (unless in the root context). 这就是为什么您不能使用/ (除非在根上下文中)。

You can use something like 您可以使用类似

 value="#{facesContext.externalContext.context.contextPath}/#{bean.path}"

to specify that the start of the path is the context root, not the server root. 指定路径的开始是上下文根,而不是服务器根。

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

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