PrimeFaces graphicImage not displaying JPEG image

2019-07-24 04:38发布

问题:

This question already has an answer here:

  • Display dynamic image from database with p:graphicImage and StreamedContent 4 answers

I am trying to display a simple JPEG image using PrimeFaces (2.2.1) graphicImage tag as the following .xhtml segment shows:

<f:metadata>
    <f:viewParam name="contractorid" value="#{logosBean.contractorID}" />
</f:metadata>
<body>
    <ui:composition template="./templates/userLayout.xhtml">
        <ui:define name="userContent">
            <f:event type="preRenderView" listener="#{logosBean.initialize}" />
            <h:form>
                <p:graphicImage value="#{logosBean.companyLogo}" alt="No Company Logo Set" />
            </h:form>
        </ui:define>
    </ui:composition>
</body>

The image is retrieved from a BLOB column in a DB2 database in the preRenderView event and stored in a byte[] until such time as logosBean.getCompanyLogo(), which consists of the following code, is invoked:

return( (companyLogo != null) ? new DefaultStreamedContent( new ByteArrayInputStream( companyLogo ) ) : null );

However, all that gets displayed is the image's alternate text. The first odd thing that is happening is that getCompanyLogo() is invoked 4 times after the preRenderView event for one refresh of the page. In the debugger, though, companyLogo contains the correct data and is returning a non-null StreamedContent object to PrimeFaces on each invocation. I am getting no errors in Glassfish's server.log. The same image displays on the page fine when I reference it as a file rather than pull it from the database. I have no idea what is going wrong nor do I have any idea how to debug it deeper. Any hints would be greatly appreciated.

回答1:

I found that changing my backing bean from ConversationScoped to SessionScoped works around the problem, but there's definitely something wrong somewhere as the following analysis shows.

I decided to take a look at the generated page to see if it might provide any clues, and in there was the <img> tag for my image:

<img id="j_idt29:j_idt30"
src="/EnergySolutionsProfile/faces/MaintainLogos.xhtml?cid=1&amp;primefacesDynamicContent=logosBean.companyLogo"
alt="No Company Logo Set" />

In Firefox's Source display window it created a link for the src attribute, so I clicked on it and got another page of source that included the following error:

org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.ConversationScoped
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:664)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:77)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:87)
at com.MSCA.ESP.WebApp.org$jboss$weld$bean-web-ManagedBean-class_com$MSCA$ESP$WebApp$LogosBean_$$_WeldClientProxy.getCompanyLogo(org$jboss$weld$bean-web-ManagedBean-class_com$MSCA$ESP$WebApp$LogosBean_$$_WeldClientProxy.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:55)
at org.primefaces.application.DynamicContentStreamer.streamDynamicContent(DynamicContentStreamer.java:66)
at org.primefaces.application.DynamicContentStreamer.beforePhase(DynamicContentStreamer.java:57)
at com.sun.faces.lifecycle.Phase.handleBeforePhase(Phase.java:228)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:99)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)

I get the same error whether or not I actually begin a conversation (e.g. in the preRenderView event). I don't know if there's some kind of incompatibility between PrimeFaces' graphicImage and Glassfish, between PF and Conversation scopes, or somewhere else. I'd like to submit an error report to someone, but I'm not even sure who the org.jboss classes belong to. I guess I will start with a comment in the PrimeFaces forum.