Search code examples
jsfgraphicimage

How to render <p:graphicImage> conditionally depending on if the resource exists


How can I render a <p:graphicImage> only if the resource URL is valid?

<p:graphicImage url="#{resource['images/image.png']}" />

Basically, I would like to render my graphicImage only if #{resource['images/image.png']} actually exists. How can I validate this? I tried to follow this JavaScript example, but I didn't succeed.


Solution

  • Check if ResourceHandler#createResource() doesn't return null.

    <p:graphicImage name="images/image.png" rendered="#{not empty facesContext.application.resourceHandler.createResource('images/image.png')}"/>
    

    Note that I replaced url="#{resource['resourceName']}" by a much simpler name="resourceName".