0

试图重现以下参考Load image from outside webapp,我无法在 xhtml 页面上显示来自以下 url http://localhost:8080/images/eventHead1.jpg的图像。

单独的 url 工作正常:Wildfly 服务提供位于standalone\data\images 文件夹中的图像,直接在浏览器上没有任何问题。但是由于以下错误,无法在 xhtml 页面上呈现图像。

我怀疑我可能没有正确定义的 Bean,尤其是参数“ pathString ”,因为 getImage 方法返回 null。感谢你的帮助。

使用版本:Mojarra:2.3.9;Primefaces:7.0;野蝇:16

错误:

23:08:42,869 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /Intranet/dashboard.xhtml: javax.servlet.ServletException: Method not found: class com.org.ui.ImageBean.getImage(null)

独立的.xml:

    <server name="default-server">
        <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
        <https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" enable-http2="true"/>
        <host name="default-host" alias="localhost">
            <location name="/" handler="welcome-content"/>
            <location name="/images" handler="ContentDir"/>      //added
            <http-invoker security-realm="ApplicationRealm"/>
        </host>
    </server>
    <servlet-container name="default">
        <jsp-config/>
        <websockets/>
    </servlet-container>
    <handlers>
        <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
        <file name="ContentDir" path="${jboss.home.dir}/standalone/data/images" directory-listing="true"/>  // added
    </handlers>

豆:

@ManagedBean
@RequestScoped

public class ImageBean {

        public StreamedContent getImage() throws IOException {
            FacesContext context = FacesContext.getCurrentInstance();

            if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
                // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
                return new DefaultStreamedContent();
            }
            else {
                // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
                String filename = context.getExternalContext().getRequestParameterMap().get("filename");

                String pathString = "http://localhost:8080/images/";

                return new DefaultStreamedContent(new FileInputStream(new File(pathString, filename)));
            }
        }
}

xhtml:

<p:graphicImage value="#{imageBean.image}">                 
   <f:param name="filename" value="#{myevent.imageName}" />       
</p:graphicImage>
4

0 回答 0