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

TCM*_*TCM 0 java jsf

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

private String getPhotoUnavailablePath = "/Common/Images/PhotoNotAvailable.jpg";
    private String getPhotoUnavailablePath(){
        return photoUnavailablePath;
    }
Run Code Online (Sandbox Code Playgroud)

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

<h:outputLink id ="blogPhoto" value="#{BlogBean.photoUnavailablePath}" >
  <h:graphicImage value="#{BlogBean.photoUnavailablePath}" style="width: 180px;height: 180px;"/>
</h:outputLink>
Run Code Online (Sandbox Code Playgroud)

我在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>
Run Code Online (Sandbox Code Playgroud)

只是把..它的作品!任何人都可以解释我发生了什么,我该如何解决这个问题?

Boz*_*zho 7

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

你可以使用类似的东西

 value="#{facesContext.externalContext.context.contextPath}/#{bean.path}"
Run Code Online (Sandbox Code Playgroud)

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