在JSF中将InputStream显示为动态图像 [英] Show an InputStream as dynamic image in JSF

查看:115
本文介绍了在JSF中将InputStream显示为动态图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在带有OmniFaces的JSF 2.0中嵌入和显示InputStream作为动态图像?

How can we embed and show an InputStream as dynamic image in JSF 2.0 with OmniFaces?

推荐答案

OmniFaces对此没有提供任何实用程序—

OmniFaces does not offer any utility for this — yet.

如果映像文件位于磁盘文件系统上,则需要在servlet容器配置中创建一个虚拟上下文,以便可以通过常规URL访问它们.另请参见从webapps/webcontext外部加载图像/使用< h:graphicImage>部署文件夹或< img>标签.

If the image files are located on the disk file system, then you need to create a virtual context in the servletcontainer configuration so that they can be accessed by a normal URL. See also Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag.

如果图像文件存储在DB中,则需要创建一个简单的servlet,该servlet仅将图像的InputStream从DB写入HTTP响应的OutputStream中.另请参见如何检索和在JSP页面中显示数据库中的图像?(请注意,这不需要JSP/HTML,您可以使用<h:graphicImage>就像生成HTML <img>元素一样好).

If the image files are stored in the DB, then you need to create a simple servlet which just writes the InputStream of the image from the DB to the OutputStream of the HTTP response. See also How to retrieve and display images from a database in a JSP page? (note that this doesn't require JSP/HTML, you can as good just use <h:graphicImage> as it merely generates a HTML <img> element).

有PrimeFaces <p:graphicImage>,但是实现起来有点不直观.另请参见使用p:graphicImage和StreamedContent显示来自数据库的动态图像.

There's the PrimeFaces <p:graphicImage>, but this is a bit unintuitive to implement. See also Display dynamic image from database with p:graphicImage and StreamedContent.

更新:由于OmniFaces 2.0(适用于JSF 2.2),因此存在 组件,该组件尤其支持直接引用@ApplicationScoped托管Bean中的byte[]InputStream属性,如下所示:

Update: since OmniFaces 2.0 (for JSF 2.2) there's a <o:graphicImage> component which supports among others directly referencing a byte[] or InputStream property in an @ApplicationScoped managed bean in an intuitive way like below:

<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方法本身.另请参见展示柜

In contrary to PrimeFaces <p:graphicImage>, no <f:param> is needed and the EL method arguments are evaluated during render response phase, but the EL method itself is only invoked when the browser actually requests the image. See also the showcase and the blog on the subject.

这篇关于在JSF中将InputStream显示为动态图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆