Search code examples
htmljakarta-eenetbeansglassfish-4graphicimage

Using a relative path as the value in a h:graphicImage tag


I'm using the h:graphicImage tag in HTML to load an image in a directory different from my current directory.

For example, say my working directory is:

/Users/nameOfUser/NetBeansProjects/MyProject/web

and the image is located in directory:

/Users/nameOfUser/Downloads/glassfish4/glassfish/domains/domain1/config/image.png

The tag is located in myHtml.xhtml (this file is in the web folder) and is as follows:

<h:graphicImage value="../../../Downloads/glassfish4/glassfish/domains/domain1/config/image.png"/>

I know that the path is correct by checking on command line, however I am receiving an IllegalArgumentException. I tried changing the path so that it was not a relative path. This solved the error however I need a relative path in order to locate the image properly.

Could anyone advise as to why this could be happening? Does this tag not accept relative paths?

Thanks in advance!

EDIT: I am using library:

xmlns:h="http://xmlns.jcp.org/jsf/html"

Solution

  • You'll need to copy your image inside the context of your app, in resources or in directory available from JSF. For example, create folder images next to your JSF page and load it with:

    <h:graphicImage value="/images/image.png"></h:graphicImage>
    

    example

    Or if you really need to load/stream it from disk, check this question: Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag