Search code examples
jsfomnifaces

GraphicImage: How to use a default image if value attribute would return null?


So what I am trying to achieve is, that my default image from the resource library is used, whenever the value attribute couldn't reference a proper image (e.g. no image uploaded/no image in the database).

Just to visualize it:

The user hasn't provided a profile pic, so bean.icon should be null.

<o:graphicImage value="#{bean.icon}" dataURI="true" /> (null isn't allowed as return value)

Now I would like the graphicImage component to display the default instead, semantically spoken:

<o:graphicImage name="img/default.png" dataURI="true" />

Is it possible to achieve this in an elegant way, perhaps without the use of JavaScript?


Solution

  • Not via <o:graphicImage> component. You could however achieve this in the bean with help of Faces#getResourceAsStream() utility.

    public InputStream getIcon() {
        InputStream input = yourIconService.getIconAsInputStreamSomehow();
        return (input != null) ? input : Faces.getResourceAsStream("/resources/img/default.png");
    }