4

How can I render a <p:graphicImage> only if the resource URL is valid?

<p:graphicImage url="#{resource['images/image.png']}" />

Basically, I would like to render my graphicImage only if #{resource['images/image.png']} actually exists. How can I validate this? I tried to follow this JavaScript example, but I didn't succeed.

Community
  • 1
  • 1
unpix
  • 853
  • 2
  • 15
  • 31

2 Answers2

3

Check if ResourceHandler#createResource() doesn't return null.

<p:graphicImage name="images/image.png" rendered="#{not empty facesContext.application.resourceHandler.createResource('images/image.png')}"/>

Note that I replaced url="#{resource['resourceName']}" by a much simpler name="resourceName".

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • BalusC, I am sorry to bother you, but this code in my local machine works perfectly, however when it's gonna alive it throws an exception of org.apache.el.parser.ParseException javax.servlet.ServletException: /component/controls/myprofile.xhtml @20,207 rendered="#{not empty facesContext.application.resourceHandler.createResource('images/image.png')}" Error Parsing: #{not empty facesContext.application.resourceHandler.createResource('images/image.png')} javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) root cause Do you have some clue why I am getting this exception? Thank you – unpix Sep 01 '14 at 14:07
  • 1
    Apparently your development environment versions don't match those of the target environment. This is only supported since EL 2.2. You can install JBoss EL to get it to work anyway on EL 2.1. See also http://stackoverflow.com/questions/3284236/invoke-direct-methods-or-methods-with-arguments-variables-parameters-in-el/3284328#3284328 It's not only that, you should **really** align out API versions in dev and production. – BalusC Sep 01 '14 at 14:08
  • There is another "solution" for previous versions to EL 2.2 for my problem, or the best is to request an upgrading to the target environment? Thank you very much @BalusC for your prompt reply and kind help! – unpix Sep 01 '14 at 14:12
  • Could we check the existing image using EL in the case when we use for rendering external files by ImageServlet? – Andrei Smirnov Sep 03 '20 at 13:37
-1

I don't use url, rather combination of name and library...

This works for me .... Hope it helps

<h:dataTable id="image" value="#bb.getCurrentBorrowerImage(history.borrower_id)}" var="i" >
<h:column rowHeader="">
<h:graphicImage name="#{i.imageFileName}" library="borrower_images" alt="Item Image"   height="100" width="100" rendered="#{!empty i.imageFileName}"/>
</h:column>

emm
  • 19
  • 1
  • 6