2

I want to display external image files under /var/images. To do that, I added another image handler "img" subsystem for the images folder in my configuration/standalone.xml as follows. Now I can browse the folder through 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>

However, I can't display it correctly in my JSF code below. The image isn't displayed in the html page in browser. Any idea of what's missing or wrong? Thank you.

<!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>
user697911
  • 10,043
  • 25
  • 95
  • 169

1 Answers1

3

The <h:graphicImage> is insuitable for the purpose of serving an image from an external URL. It interprets the value as a context-relative URL and auto-prepends the webapp's context path to the given value.

In other words, when the webapp is deployed to context path of /somecontext, then the

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

generates

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

You should have noticed that by inspecting the generated HTML output and the 404 errors on img src in browser's console (press F12 in Chrome/Firefox23+/IE9+).

Just use plain HTML <img> instead of <h:graphicImage>. It serves no additional benefits in your specific case. Using a JSF component would only add unnecessary overhead.

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

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I don't feel like it's really answering the question. In glassfish you can serve static content located somewhere else on the disk like in your first link you posted. So it's the same issue with glassfish but one can make it work. So it should be the same for wildfly isn't it ? – Ced Nov 01 '15 at 17:34
  • 1
    No, it's the same as with Tomcat. – BalusC Nov 01 '15 at 18:22
  • @BalusC, in the first link, why it says: "or in JSF as follows (context path is automatically prepended) ". And that's why I think my approach above should work with JSF. – user697911 Nov 02 '15 at 18:56
  • @user697911: that was for the GlassFish approach (it will publish the virtual context in the very same webapp context, not as a separate webapp like Tomcat and WildFly). – BalusC Nov 02 '15 at 18:56
  • I got it. Thanks a lot. – user697911 Nov 02 '15 at 19:04