简体   繁体   English

如何在 JSF 2.0 中渲染图像

[英]How to render images in JSF 2.0

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:根据 JSF 2.0 规范,根据 JSF 生成 HTML 标签的“src”属性的方式,可以使用三种方式使用 h:graphicImage:

<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:在我的 JSF 实现(MyFaces 2.0.2)中,这里是生成的 output HTML:

<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.因此,如果我使用 (name, library) 或 (value) 属性,图像总是将由 JSF 的 servlet 流式传输到客户端。 If I use (url) attribute, I can give direct link to the resource with no servlet intervention.如果我使用 (url) 属性,我可以直接链接到资源而无需 servlet 干预。

For me, the second approach - direct server URL to resource, is faster.对我来说,第二种方法 - 将服务器 URL 直接连接到资源,速度更快。

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.对我来说,第二种方法 - 将服务器 URL 直接连接到资源,速度更快。

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). “直接服务器 URL 到资源”方法也使用 servlet(容器提供的default servlet)。 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.它允许您从 JAR 文件中提供资源。 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.library实际上应该指向具有所有 CSS/JS/图像的公共资源库,而不是特定的“图像”库。

Related questions:相关问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM