Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render images in JSF 2.0

Tags:

image

jsf

jsf-2

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?

like image 571
Irina Marudina Avatar asked Aug 17 '11 11:08

Irina Marudina


1 Answers

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:

  • How to reference JSF image resource as CSS background image url
  • Changing JSF prefix to suffix mapping forces me to reapply the mapping on CSS background images
like image 122
BalusC Avatar answered Sep 19 '22 14:09

BalusC