【问题标题】:How do i refer to any image in my java web application from root of my web application?如何从我的 Web 应用程序的根目录引用我的 Java Web 应用程序中的任何图像?
【发布时间】:2010-05-03 07:48:26
【问题描述】:

我有一个名为 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>

只需输入.. 即可!谁能解释一下发生了什么以及如何解决这个问题?

【问题讨论】:

    标签: java jsf


    【解决方案1】:

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

    你可以使用类似的东西

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

    指定路径的开始是上下文根,而不是服务器根。

    【讨论】:

    • 很好的答案 Bozho :)。谢谢
    猜你喜欢
    • 2019-05-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 2012-03-07
    • 2019-02-12
    • 2012-12-04
    相关资源
    最近更新 更多