我们如何使用 OmniFaces 在 JSF 2.0 中嵌入并显示 InputStream
作为动态图像?
最佳答案
OmniFaces 尚未为此提供任何实用程序。
如果图像文件位于磁盘文件系统上,那么您需要在 servletcontainer 配置中创建一个虚拟上下文,以便可以通过普通 URL 访问它们。另请参阅Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag .
如果图像文件存储在数据库中,那么您需要创建一个简单的 servlet,仅写入 InputStream
图像从数据库到 OutputStream
HTTP 响应。另请参阅How to retrieve and display images from a database in a JSP page? (请注意,这不需要 JSP/HTML,您可以直接使用 <h:graphicImage>
,因为它仅生成 HTML <img>
元素)。
这是 PrimeFaces <p:graphicImage>
,但这实现起来有点不直观。另请参阅Display dynamic image from database with p:graphicImage and StreamedContent .
更新:自 OmniFaces 2.0(针对 JSF 2.2)以来,有一个 <o:graphicImage>
支持直接引用 byte[]
的组件或InputStream
属性(property) @ApplicationScoped
托管 bean 的直观方式如下:
<o:graphicImage value="#{imageStreamer.getById(bean.imageId)}" />
@Named
@ApplicationScoped
public class ImageStreamer {
@Inject
private ImageService service;
public InputStream getById(Long id) { // byte[] is also supported.
return service.getContent(id);
}
}
与 PrimeFaces 相反 <p:graphicImage>
,否<f:param>
需要并且 EL 方法参数在渲染响应阶段进行评估,但 EL 方法本身仅在浏览器实际请求图像时才会被调用。另请参阅the showcase和 the blog on the subject .
关于jsf-2 - 在 JSF 中将 InputStream 显示为动态图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11649524/