在 JSF 中显示 Wildfly 外部的外部图像

Display external images outside of Wildfly in JSF

我想在/var/images下显示外部图像文件。为此,我为 configuration/standalone.xml 中的图像文件夹添加了另一个图像处理程序 "img" 子系统,如下所示。现在我可以通过 http://localhost:8080/img/test.jpg 浏览文件夹。

<server name="default-server">
                <http-listener name="default" socket-binding="http"/>
                <host name="default-host" alias="localhost">
                    <location name="/" handler="welcome-content"/>
                    **<location name="/img" handler="images"/>**
                        <filter-ref name="server-header"/>
                    <filter-ref name="x-powered-by-header"/>
                </host>
            </server>
            <servlet-container name="default">
                <jsp-config/>
                <websockets/>
            </servlet-container>
            <handlers>
                <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
                **<file name="images" path="/var/images" directory-listing="true"/>**
            </handlers>

但是,我无法在下面的 JSF 代码中正确显示它。图片未显示在浏览器的 html 页面中。知道缺少什么或错了什么吗?谢谢。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:p="http://primefaces.org/ui" 
    >

<ui:composition template="template.xhtml">
    <ui:define name="content">
        <h:messages />

        <h:form id="myform">
            <h:panelGrid columns="5">

                <h:graphicImage value="/img/test.jpg" />
                ...
            </h:panelGrid>
        </h:form>
        <br />


    </ui:define>
</ui:composition>
</html>

<h:graphicImage> 不适合从外部 URL 提供图像。它将 value 解释为上下文相关的 URL 并自动将 webapp 的上下文路径添加到给定的 value.

换句话说,当 webapp 被部署到 /somecontext 的上下文路径时,那么

<h:graphicImage value="/img/test.jpg" />

生成

<img src="/somecontext/img/test.jpg" />

您应该已经注意到,通过检查生成的 HTML 输出和浏览器控制台中 img src 上的 404 错误(在 Chrome/Firefox23+/IE9+ 中按 F12)。

只需使用普通 HTML <img> 而不是 <h:graphicImage>。在您的特定情况下,它不会提供额外的好处。使用 JSF 组件只会增加不必要的开销。

<img src="/img/test.jpg" />

另请参阅:

  • Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag
  • EL context path evaluation difference between outputLink and graphicImage