Search code examples
jsfjsf-2primefaces

Dynamic creation of linked image with JSF 2.2 (Primefaces 5.0)


JSF 2.2, Primefaces 5.0,

What I want is just an image which links to a website. This should be generated using Java.

JSF:

<ui:component binding="#{bean.content}" />

Bean:

public UIComponent getContent() {
    GraphicImage image = (GraphicImage) FacesContext.getCurrentInstance().getApplication().createComponent(GraphicImage.COMPONENT_TYPE);
    image.setValue(imagePath);

    Link link = (Link) FacesContext.getCurrentInstance().getApplication().createComponent(Link.COMPONENT_TYPE);
    link.setHref(uri);

    ...
}

How can I put an image into the link tag?

I tried link.setValue(image). It didn't work. Just got the toString representation of the image object.

The result HTML should be something like:

<a href="www.uri.com"><img src="image.jpg"></a>


Solution

  • Something like this works..

    public UIComponent getContent() {
    GraphicImage image = (GraphicImage) FacesContext.getCurrentInstance().getApplication().createComponent(GraphicImage.COMPONENT_TYPE);
    image.setUrl("yourimage.jpg");
    image.setOnclick("window.open('http://stackoverflow.com');");
    return image;
    }
    

    Change the path and url accordingly.