Search code examples
jsf-2glassfishmyfaces

dynamically load image names from database and display them using jsf 2.0


i'm trying to find a solution to load image names from a database and display them on a webpage. Basically, when i display an image i do it like this:

<h:graphicImage value="#{resource['mywebapp:image.png']}">
</h:graphicImage>

So now instead of image.png i would like to dynamically load alternative images. Problem is, that i can't use an EL nested expression inside another one (at least i have not found out how). To put the logic into the backing bean also does not help in this case, because then it is rendered like that:

 <img src="#{resource['mywebapp:somedynamicallyloadedimagename.png']}">
</img>

The only thing that works, is to directly generate the path in the backing bean:

public String getImage() {
    this.image = "/mywebapp/javax.faces.resource/"+this.image+".jsf?ln=images";
            return image;
}                                       

However this "solution" is far from elegant, since everything is hardcoded and this is nothing i'm going to put in a production app. What would be a good solution to do this? I would like to avoid putting the images into the database and using primefaces.. Since i'm not an experienced JSF coder (as you can probably tell), i would highly appreciate some thoughts on this topic...thanks in advance!


Solution

  • Try

    <h:graphicImage library="images" name="#{generated.name}" />