Basically my question is the same as this one answered five years ago: I would like to output the URL of a server-provided image as from <p:graphicImage>
, but just the URL and not an <img>
tag. What I am looking for is a solution like this:
(This is not working code, just to illustrate:)
<p:graphicImage var="url" value="#{MyForm.image}"/>
<span id="imageData" data-image="#{url}/>
Which should output to the HTML like this:
<span id="imageData" data-image="http://localhost:8080/contextPath/javax.faces.resource/dy...
I am currently using a JavaScript work-around for this, but this requirement seems so common to me that I would expect newer development has already targeted it and there is a built-in solution. Is there news on that topic since the last five years?
Just took a look at the GraphicImageRenderer wanting to figure out how they produce that dynamic URL. They are using a static method on their DynamicContentSrcBuilder:
So why not just reproduce this call on the
p:graphicImage
component? I'll firstbind
it to a property of aRequestScoped
bean and then get the URI from that bean which reproduces behavior of the renderer shown above:The XHTML code:
And my bean aka
myBean
:Alternatively we can use the
GraphicImageRenderer
and publish theprotected
methodgetImageSrc
:But why did I write
#{myBean.getImgUri()}
instead of#{myBean.imgUri}
? I'm still not sure, but using the latter produces aPropertyNotFoundException
:Possibly invoking the
DynamicContentSrcBuilder.build()
method somehow messes up the ongoing resolving of the value expression#{myBean.imgUri}
, while#{myBean.getImgUri()}
is not affected.Getting back to your question: no there is no news. The dynamic URI is built and directly sent to the browser while rendering. The
GraphicImage
component does not provide any getter for this. What I presented here is a server side variant of a work around without the need to create a new servlet.Edit:
Of course you can also parameterize the
getImageUri
method and bind your Image component to some arbitrary variable which would bring you closer to your demonstrationvar="..."
example:Then use this method by:
This way there's no need to create a bean or method for each image.