![](/img/trans.png)
[英]Image Display Using p:graphicImage. Pictures appear like negatives
[英]Display a BLOB image using p:graphicImage in a tagfile
使用<p:graphicImage>
顯示BLOB圖像,如下所示。
<p:graphicImage value="#{categoryBean.image}">
<f:param name="id" value="7"/>
</p:graphicImage>
其中CategoryBean
已定義如下。
@Named
@ApplicationScoped
public class CategoryBean {
@Inject
private CategoryService service;
public CategoryBean() {}
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
} else {
String id = context.getExternalContext().getRequestParameterMap().get("id");
byte[] bytes = Utils.isNumber(id) ? service.findImageById(Long.parseLong(id)) : null;
return bytes == null ? null : new DefaultStreamedContent(new ByteArrayInputStream(bytes));
}
}
}
關於上述方法,以下自定義標記應該可以正常工作,但不能在<p:graphicImage>
上顯示圖像,沒有錯誤/異常。
<my:image bean="#{categoryBean}" property="image" paramName="id" paramValue="7"/>
標記文件位於/WEB-INF/tags/image.xhtml
下。
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<p:graphicImage value="#{bean[property]}">
<f:param name="#{paramName}" value="#{paramValue}"/>
</p:graphicImage>
</ui:composition>
生成的<img>
標記看起來不錯:
<img id="form:j_idt4"
src="/ContextPath/javax.faces.resource/dynamiccontent.properties.xhtml?ln=primefaces&v=5.3&pfdrid=IA0%2F7ZuBnGS%2BSzeb%2BHyPOTo4Pxp4hjI6&pfdrt=sc&id=7&pfdrid_c=true"
alt=""/>
它僅返回HTTP 404錯誤。
給定的自定義標簽的定義是否存在任何缺陷?
這是由PrimeFaces <p:graphicImage>
識別圖像請求的方式引起的。 基本上,它將精確值表達式#{bean[property]}
為字符串,對其進行加密,然后將其作為pfdrid
值傳遞。 當網絡瀏覽器需要通過全新的HTTP請求下載圖像時,該值表達式將在“當前” EL上下文中解密和評估。 但是,在那一刻,在EL上下文中的任何地方都沒有#{bean}
或#{property}
可用,因為無法使用帶有標記文件和所有標記的JSF視圖。 EL上下文中僅提供請求,會話和應用程序范圍的Bean。
除了向PrimeFaces員工報告問題外,沒有其他針對性。
至於替代解決方案, OmniFaces <o:graphicImage>
在此方面做得更好,方法是在渲染響應期間而不是在流圖像期間檢查目標bean /方法。 它立即檢查#{bean[property]}
,發現它實際上代表#{categoryBean.image}
,然后成功。 只是要確保我像您一樣在一個標記文件中對其進行了測試,並且它對我來說很好用,而PF確實確實按照所述失敗了。
<o:graphicImage value="#{bean[property](paramValue)}" />
public byte[] getImage(Long id) throws IOException {
return service.findImageById(id);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.