14

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?

Irina Marudina
  • 233
  • 1
  • 2
  • 6

1 Answers1

9

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.

Related questions:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • How to set the attribute `alt` for HTML tag `img` in this case? For example `` doesn't work... – Michael Jun 05 '12 at 12:28
  • @Michael: please press `Ask Question` button on right top whenever you have new and unrelated question. – BalusC Jun 05 '12 at 12:30
  • 1
    Ok no problem: http://stackoverflow.com/questions/10897493/jsf-2-0-how-to-set-the-attribute-alt-for-image – Michael Jun 05 '12 at 12:40