【发布时间】:2020-06-06 02:23:15
【问题描述】:
我使用流式内容从后端数据库生成动态图像。与 p:graphicImage 一起使用时它们可以正常工作。
当这样的动态图片需要作为div的背景时,问题就来了。
如何使用通过流内容创建的动态图像作为 div 元素的背景?
JSF 控制器
public StreamedContent imageByCodeInlineWithoutGet(String code) {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getRenderResponse()) {
return new DefaultStreamedContent();
} else {
String id = code;
if (id == null) {
return new DefaultStreamedContent();
}
String j = "select s from Upload s where lower(s.code)=:id";
Map m = new HashMap();
m.put("id", id.trim().toLowerCase());
Upload temImg = getUploadFacade().findFirstByJpql(j, m);
if (temImg != null) {
byte[] imgArr = null;
try {
imgArr = temImg.getBaImage();
} catch (Exception e) {
return new DefaultStreamedContent();
}
if (imgArr == null) {
return new DefaultStreamedContent();
}
StreamedContent str = new DefaultStreamedContent(new ByteArrayInputStream(imgArr), temImg.getFileName());
return str;
} else {
return new DefaultStreamedContent();
}
}
}
动态生成图像
<p:graphicImage cache="false" value="#{streamedContentController.imageByCodeInlineWithoutGet('header__logo')}" >
</p:graphicImage>
我想将图像用作 CSS 背景,如下所示
<style type="text/css">
background-image:#{streamedContentController.imageByCodeInlineWithoutGet('header__logo')};
</style>
或作为参数传递。
<div class="hero__item set-bg" data-setbg="#{streamedContentController.imageByCodeInlineWithoutGet('header__logo')}">
</div>
</div>
是否可以使用 JSF 或 PrimeFaces 或任何其他库?
【问题讨论】:
标签: jsf primefaces