According to the JSF 2.0 specification, there are three ways to use h:graphicImage depending on the way by which JSF generates the "src" attribute of the HTML tag:
<h:graphicImage value="#{resource['images:next.gif']}"/>
<h:graphicImage library="images" name="next.gif"/>
<h:graphicImage url="/resources/images/next.gif"/>
The specification states that the first two should render exactly the same markup. In my JSF implementation (MyFaces 2.0.2), here is the output HTML that is generated:
<img src="/AppName/faces/javax.faces.resource/next.gif?ln=images">
<img src="/AppName/faces/javax.faces.resource/next.gif?ln=images">
<img src="/AppName/resources/images/next.gif">
So it seems that if I use (name, library) or (value) attributes, the image is always going to be streamed to the client by JSF's servlet. If I use (url) attribute, I can give direct link to the resource with no servlet intervention.
For me, the second approach - direct server URL to resource, is faster.
In what cases the first approach - specifying (name, library) or (value) attributes, be used?
For me, the second approach - direct server URL to resource, is faster.
The difference should be totally negligible. The "direct server URL to resource" approach also uses a servlet (the default
servlet which is provided by the container). Please show your benchmark results.
In what cases the first approach - specifying (name, library) or (value) attributes, be used?
It allows you for serving the resources from within a JAR file. It also allows you for a nicer way of dynamically switching the library in the following manner:
<h:graphicImage library="#{user.prefs.looknfeel}" name="next.gif"/>
The library
should actually point to a common resource library with all CSS/JS/images, not to a specific "images" library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With