在<ui:repeat>里面的<p:graphicImage>中显示数据库blob图像

Gon*_*gui 32 jsf dynamic-content primefaces uirepeat graphicimage

我在JBoss 7.1.1上使用PrimeFaces 3.2.

我试图在MySQL数据库中显示存储在BLOB中的图像<ui:repeat>.图像存储在a中byte[],然后转换StreamedContent为如下:

InputStream stream = new ByteArrayInputStream(ingredient.getImage());
ingredient.setJsfImage(new DefaultStreamedContent(stream, "image/jpg"));
Run Code Online (Sandbox Code Playgroud)

然后我试图在Facelet中显示它如下:

<ui:repeat var="ingredient" value="#{formBean.ingredientResultSet}">
    <p:panel id="resultsPanel" header="#{ingredient.location.shopName}">
        <p:graphicImage value="#{ingredient.jsfImage}" alt="No picture set" />
...
Run Code Online (Sandbox Code Playgroud)

但是,在加载页面时,我在JBoss中收到以下错误:

SEVERE [org.primefaces.application.PrimeResourceHandler](http - 127.0.0.1-8080-12)流式动态资源出错.

这是怎么造成的,我该如何解决?

Bal*_*usC 54

您需要意识到<p:graphicImage>实际上<img src>只使用一个URL 呈现一个元素,然后当它要解析获得的HTML标记并显示结果时,Web浏览器随后会单独调用该URL.

因此,无论你在getter方法中做什么,都<p:graphicImage>必须按照每个请求调用它的方式进行设计.因此,最理智的方法是创建一个<p:graphicImage>a,<f:param>其中<p:graphicImage value>points指向一个完全独立的请求或应用程序作用域bean,并<f:param value>指向唯一的图像标识符.

例如

<p:graphicImage value="#{images.image}">
    <f:param name="id" value="#{someBean.imageId}" />
</p:graphicImage>
Run Code Online (Sandbox Code Playgroud)

Images支持bean可以是这样的:

@ManagedBean
@ApplicationScoped
public class Images {

    @EJB
    private ImageService service;

    public StreamedContent getImage() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
            return new DefaultStreamedContent();
        }
        else {
            // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
            String id = context.getExternalContext().getRequestParameterMap().get("id");
            Image image = service.find(Long.valueOf(id));
            return new DefaultStreamedContent(new ByteArrayInputStream(image.getBytes()));
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

或者,如果您已经在使用OmniFaces 2.0或更新版本,那么请考虑使用它<o:graphicImage>,它可以更直观地使用,几乎可以按照您的预期方式使用.另请参阅有关该主题的博客.

也可以看看: