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

时间:2010-05-03 07:48:26

标签: java jsf

我有一个名为Common/Images/Photounavailable.jpg的文件夹。这是从根目录引用图像的正确方法吗?我想从我的Web应用程序的任何文件夹中的任何页面引用此图像。

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

从我的JSF页面我写了类似的东西: -

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

我在h:graphicImage中看到图像。但是当我点击它时,我得到404错误。 OutputLink无法获得与图像的正确链接。这怎么可能?那个h:graphicImage得到了正确的链接而h:outputLink没有?

此外,此代码完美无缺: -

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

只需将..置于其中即可!任何人都能解释我发生了什么,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:7)

<h:graphicImage>使用上下文相对路径 <h:outputLink>使用绝对路径。这就是为什么你不能使用/(除非在根上下文中)。

您可以使用类似

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

指定路径的起点是上下文根,而不是服务器根目录。