Alternative for p:graphicImage which can show images from byte[] and control browser cache

孤街浪徒 提交于 2019-12-01 12:06:43
BalusC

JSF utility library OmniFaces has a <o:graphicImage> which does technically a better job in streaming and caching the images. It doesn't invoke the getter method twice, but only once when the browser really needs to download the image. Also, it supports consuming a byte[] or InputStream without the need for a wrapper. All in all, you end up with clearer model.

@Named
@ApplicationScoped
public class ImageStreamer {

    @Inject
    private ImageService service;

    public byte[] getById(Long id) {
        return service.getContent(id);
    }

}

<o:graphicImage value="#{imageStreamer.getById(bean.image.id)}" />

It by default forces the browser to not cache the image, so the image will be downloaded on every request. For efficiency, a lastModified attribute can be set to allow browsers to cache the image as long as the image is unchanged as per lastModified value.

<o:graphicImage value="#{imageStreamer.getById(bean.image.id)}" lastModified="#{bean.image.lastModified}" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!