如何在JSF 2.0中呈现图像 [英] How to render images in JSF 2.0
问题描述
根据JSF 2.0规范,有三种方法可以使用h:graphicImage,具体取决于JSF生成HTML标记的src属性的方式:
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"/>
规范声明前两个应该呈现完全相同的标记。在我的JSF实现(MyFaces 2.0.2)中,这里是生成的输出HTML:
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">
所以看来如果我使用(名称,库)或(值)属性,图像是总是要通过JSF的servlet流式传输到客户端。如果我使用(url)属性,我可以直接链接到没有servlet干预的资源。
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.
对我来说,第二种方法 - 直接服务器URL到资源,是更快。
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?
推荐答案
对我来说,第二种方法 - 将资源的直接服务器URL更快。
差异应该完全可以忽略不计。 直接服务器URL到资源方法也使用servlet(容器提供的 default
servlet)。请显示您的基准测试结果。
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.
在什么情况下第一种方法 - 指定(名称,库)或(值)属性,是否可以使用?
它允许您从内部提供资源一个JAR文件。它还允许您以下列方式更好地动态切换库:
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"/>
库
实际上应指向一个共同点包含所有CSS / JS /图像的资源库,而不是特定的图像库。
The library
should actually point to a common resource library with all CSS/JS/images, not to a specific "images" library.
- 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
这篇关于如何在JSF 2.0中呈现图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!