3

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?

Snu
  • 142
  • 1
  • 12

1 Answers1

4

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");
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Wow! Thank you very much for the fast response. Gonna try it and accept your answer asap. – Snu Aug 01 '16 at 10:14
  • @BalusC How can i do the same in primefaces? Faces mentioned above is in the omnifaces util package – raikumardipak Feb 21 '18 at 09:55
  • @BalusC Plz also have a look at this https://stackoverflow.com/questions/48860434/why-file-exists-does-not-work. I am trying to achieve a similar thing wherein i check if the file (image file, to be specific but generalised for the Question post) exists then i'll perform further action. However as pointed out in comments of that Question the file exists in a vfs (the deployment folder of the server where the webapp is running) and so always returns false. Any help would be great. Thanks – raikumardipak Feb 21 '18 at 09:56