Board index JavaServer Faces General p:graphicImage is not working inside h:dataTable/p:dataTable

p:graphicImage is not working inside h:dataTable/p:dataTable

Components, Ajax Framework, Utilities and More.


Posts: 13
Hi,
I found out that p:graphicImage is not working inside h:dataTable (PrimeFaces 2.1 on GlassFish 3.0.1.)
follows some partial extract from test code:
----------
...
<h:dataTable value="#{companyController.items}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListCompanyTitle_name}"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListCompanyTitle_photo}"/>
</f:facet>
<p:graphicImage id="photo" value="#{item.photo}" cache="FALSE" />
</h:column>
...
</h:dataTable>
...
--------------------
p:graphicImage can't recognise local variable item used for dataTable loop.
The real backing bean is companyController. When I use p:graphicImage with selected item from the backing bean everything is OK,
but in this case where the value is bind to local variable item.photo the generated src in <img> tag:
"<td><img id="j_idt12:j_idt22:0:photo" src="/test/company/List.jsf?primefacesDynamicContent=item.photo&amp;primefaces_image=6c5119cb-355b-4145-a225-0b13288eb789" alt="" /></td>"
does not return any image content,
instead it refresh the current page as a result of calling it.

Maybe this is a normal behaviour, having in mind how the things work in that case,
but if there is a chance to work around or fix, it will be nice.

Regards,
Dimitar Kazakov
Last edited by kazak on 25 Aug 2010, 13:00, edited 1 time in total.
-------------------------------------------------------------------------------------------------------------------------------------
PrimeFaces 2.2 M1 - Mojarra 2.0.2 (FCS b10) included in GlassFish v3.0.1 - Java 1.6.0_21-b07 - NetBeans 6.9.1


Posts: 13
Just to add that I tried to convert h:dataTable to p:dataTable, p:column
but there is no effect. The result is the same, p:graphicImage is not rendered.
follows extract from changed code:
----------------
<p:dataTable value="#{companyController.items}" var="item">
<p:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListCompanyTitle_name}"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListCompanyTitle_photo}"/>
</f:facet>
<p:graphicImage id="photo" value="#{item.photo}" cache="FALSE" />
</p:column>
----------------
generated img tag src:
src="/test2grid/company/List.jsf?primefacesDynamicContent=item.photo&amp;primefaces_image=f8dd09de-a467-4b2a-80c2-0b37062a0d7b"

Regards,
DImitar Kazakov
-------------------------------------------------------------------------------------------------------------------------------------
PrimeFaces 2.2 M1 - Mojarra 2.0.2 (FCS b10) included in GlassFish v3.0.1 - Java 1.6.0_21-b07 - NetBeans 6.9.1


Posts: 15115
Location: Cybertron

Dynamic images cause a new request, in your case same request is done over and over again, you need to use f:param to identify the item. This is how it's done in data iteration components.

See page 175 in User's Guide.
PrimeFaces Lead


Posts: 6

Dimitar if you solve the problem please let me now how you did it, because i have the same problem.
I tried f:param but nothing happened!
Thank you in advance...

PS: viewtopic.php?f=3&t=4102
PrimeFaces 2.1 - Mojarra 2.0.2 (FCS b10) included in GlassFish v3.0.1 - Java 1.6.0_21-b07 - NetBeans 6.9.1


Posts: 13
mvodas,
I tried with f:param, as sugested by optimus.prime and its working that way for me. But when I traced the code, I found something strange which maybe covers partially the same situation as described in your post http://primefaces.prime.com.tr/forum/viewtopic.php?f=3&t=4102

I found out, that for every single row in dataTable, the backing bean's method companyController.streamedImageById is called 3 times. First 2 times FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("client_id") evaluates to null. Only in the last third call it evaluates to the correct value passed from f:param tag, and as a result the method returns the correct image stream.

I will try to find out from which primefaces classes,methods companyController.streamedImageById is called every time and will post results later.

Here are some parts of the code I tried with:
-----
<p:dataTable value="#{companyController.items}" var="item">
<p:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListCompanyTitle_name}"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListCompanyTitle_photo}" />
</f:facet>
<p:graphicImage id="photo" value="#{companyController.streamedImageById}" cache="FALSE">
<f:param name="client_id" value="#{item.id}" />
</p:graphicImage>
</p:column>
--------
generated img tag src=src="/test2grid/company/List.jsf?primefacesDynamicContent=companyController.streamedImageById&amp;client_id=10&amp;primefaces_image=b5575d64-d481-4a64-b00a-08914e8069b7"
-----------
public StreamedContent getStreamedImageById() {
DefaultStreamedContent image = null;
String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("client_id");

Long clientId = Long.parseLong(id);
image = (DefaultStreamedContent) ejbFacade.find(clientId).getStreamedImage();

return image;
}
---------------
-------------------------------------------------------------------------------------------------------------------------------------
PrimeFaces 2.2 M1 - Mojarra 2.0.2 (FCS b10) included in GlassFish v3.0.1 - Java 1.6.0_21-b07 - NetBeans 6.9.1


Posts: 13
here are the stack traces for the tree calls:

FIRST
----------------------------------------------------------
"http-thread-pool-8080-(1)"
com.ows.web2grid.jsf.CompanyController.getStreamedImageById(CompanyController.java:242)
sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
com.sun.el.parser.AstValue.getValue(AstValue.java:116)
com.sun.el.parser.AstValue.getValue(AstValue.java:163)
com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:71)
com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
javax.faces.component.UIGraphic.getValue(UIGraphic.java:150)
org.primefaces.component.graphicimage.GraphicImageRenderer.encodeEnd(GraphicImageRenderer.java:38)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:78)
org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:62)
org.primefaces.component.datatable.DataTableRenderer.encodeTable(DataTableRenderer.java:589)
org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:492)
org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:273)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:295)
com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:106)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
com.sun.grizzly.ContextTask.run(ContextTask.java:69)
com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
java.lang.Thread.run(Thread.java:619)



SECOND
-------------------------------------------------------------------------------------
"http-thread-pool-8080-(1)"
com.ows.web2grid.jsf.CompanyController.getStreamedImageById(CompanyController.java:242)
sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
com.sun.el.parser.AstValue.getValue(AstValue.java:116)
com.sun.el.parser.AstValue.getValue(AstValue.java:163)
com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:71)
com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
javax.faces.component.UIGraphic.getValue(UIGraphic.java:150)
org.primefaces.component.graphicimage.GraphicImageRenderer.getImageSrc(GraphicImageRenderer.java:54)
org.primefaces.component.graphicimage.GraphicImageRenderer.encodeEnd(GraphicImageRenderer.java:38)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:78)
org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:62)
org.primefaces.component.datatable.DataTableRenderer.encodeTable(DataTableRenderer.java:589)
org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:492)
org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:273)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:295)
com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:106)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
com.sun.grizzly.ContextTask.run(ContextTask.java:69)
com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
java.lang.Thread.run(Thread.java:619)


and THIRD
-------------------------------------------------------------------------------
"http-thread-pool-8080-(1)"
com.ows.web2grid.jsf.CompanyController.getStreamedImageById(CompanyController.java:243)
sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
com.sun.el.parser.AstValue.getValue(AstValue.java:116)
com.sun.el.parser.AstValue.getValue(AstValue.java:163)
com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:71)
org.primefaces.application.DynamicContentStreamer.streamDynamicContent(DynamicContentStreamer.java:65)
org.primefaces.application.DynamicContentStreamer.beforePhase(DynamicContentStreamer.java:56)
com.sun.faces.lifecycle.Phase.handleBeforePhase(Phase.java:228)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:99)
com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
com.sun.grizzly.ContextTask.run(ContextTask.java:69)
com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
java.lang.Thread.run(Thread.java:619)
-------------------------------------------------------------------------------------------------------------------------------------
PrimeFaces 2.2 M1 - Mojarra 2.0.2 (FCS b10) included in GlassFish v3.0.1 - Java 1.6.0_21-b07 - NetBeans 6.9.1


Posts: 6

I managed to solve it. Dimitar your help was key to solving it, thank you again. So here is all of my code in case someone else encounters the same problem.

index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Index</title>
    </h:head>
    <h:body>
        <h:form>
            <ui:repeat value="#{SBean.imageList}" var="image">
                <p:panel header="#{image.id}">
                    <p:graphicImage value="#{SBean.image}" height="100" width="100">
                        <f:param name="image_id" value="#{image.id}"/>
                    </p:graphicImage>
                </p:panel>
            </ui:repeat>
        </h:form>
    </h:body>
</html>


Image.java
package entity;

import org.primefaces.model.StreamedContent;

public class Image {

    private Integer id;
    private StreamedContent image;

    public Image() {
    }

    public Image(Integer id, StreamedContent image) {
        this.id = id;
        this.image = image;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public StreamedContent getImage() {
        return image;
    }

    public void setImage(StreamedContent image) {
        this.image = image;
    }
}


SBean.java
package beans;

import entity.Image;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.postgresql.largeobject.LargeObject;
import org.postgresql.largeobject.LargeObjectManager;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

@ManagedBean(name = "SBean")
@SessionScoped
public class SBean {

    private HashMap<Integer, Image> images = new HashMap<Integer, Image>();

    public SBean() throws ClassNotFoundException, SQLException {
        Class.forName("org.postgresql.Driver");
        Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/eshop", "postgres", "111111");
        conn.setAutoCommit(false);

        LargeObjectManager lobj = ((org.postgresql.PGConnection) conn).getLargeObjectAPI();

        PreparedStatement ps = conn.prepareStatement("SELECT id, data FROM image");
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            Integer id = rs.getInt(1);

            long oid = rs.getLong(2);
            LargeObject obj = lobj.open(oid, LargeObjectManager.READ);

            byte[] data = new byte[obj.size()];
            obj.read(data, 0, obj.size());
            obj.close();

            images.put(id, new Image(id, new DefaultStreamedContent(new ByteArrayInputStream(data))));
        }
        rs.close();
        ps.close();
        conn.commit();
        conn.close();
    }

    public List<Image> getImageList() {
        return new ArrayList<Image>(images.values());
    }

    public StreamedContent getImage() {
        String image_id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("image_id");
        System.out.println("image_id: " + image_id);

        if (image_id == null) {
            return images.get(1).getImage(); //if you return null here then it won't work!!! You have to return something.
        }

        return images.get(Integer.parseInt(image_id)).getImage();
    }
}


Note: Dimitar this shows you were right about the bean method called 3 times. This is what I get in the server log (produced by SBean.getImage()).
INFO: image_id: null
INFO: image_id: null
INFO: image_id: null
INFO: image_id: null
INFO: image_id: null
INFO: image_id: null
INFO: image_id: 1
INFO: image_id: 3
INFO: image_id: 2
PrimeFaces 2.1 - Mojarra 2.0.2 (FCS b10) included in GlassFish v3.0.1 - Java 1.6.0_21-b07 - NetBeans 6.9.1


Posts: 15115
Location: Cybertron

Thanks for sharing.
PrimeFaces Lead


Posts: 89
is it still working with 3.0.M1 ?

my FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() ignores any param that I'm passing....


Posts: 65
I encounter a problem rendering the image. I have followed the code explained and I correctly retrieve the id for image to display in a separate method:

public StreamedContent getStreamedImage() {
      
      StreamedContent streamedImage = null;
UIViewRoot uiRoot = FacesContext.getCurrentInstance().getViewRoot();
      
      System.out.println(uiRoot.getClientId());
      
      UIComponent component = FacesContext.getCurrentInstance().getViewRoot().findComponent("itemDataForm:imagesTbl:itemImgComp");
      Map attributes = component.getAttributes();
      Integer image_id = (Integer)attributes.get("item_img_id");
      
      if(image_id != null && editionItem != null && editionItem.getImages() != null){
         for(ItemImageView img : editionItem.getImages()){
            if(image_id.intValue() == img.getId()){
               streamedImage = new DefaultStreamedContent(new ByteArrayInputStream(img.getImage()), "image/jpeg");
            }
         }
      }else{
         streamedImage = new DefaultStreamedContent(new ByteArrayInputStream(editionItem.getImages().get(0).getImage()), "image/jpeg");
      }
.......



I could't manage to retrieve the <f:param> (always null) so I tried with attribute and it works. So, the DefaultStreamContent is loaded, by the image doesn't render at all. My code for xhtml:

<p:dataTable id="imagesTbl" var="itemImg" value="#{itemEditionBean.editionItem.images}">
                              <p:column>
                                 <h:panelGrid columns="1">
                                    <h:outputText value="#{itemImg.id}"/>
                                    <p:graphicImage id="itemImgComp" value="#{itemEditionBean.streamedImage}">
                                       <f:attribute name="item_img_id" value="#{itemImg.id}"/>
                                    </p:graphicImage>
                                 </h:panelGrid>
                              </p:column>
                           </p:dataTable>



Exactly the same that the code working in this topic above. PS: I have mt dataTable enclosed within a tab. Maybe a dependency on the enclosing component, or form, or what?
----------------------------------------------------------------------------
primefaces-3.4
mojarra-2.1.2-FCS
Tomcat 7

Next

Return to General