Display images from database with dialog

UI Components for JSF
Post Reply
AFC_Primefaces
Posts: 4
Joined: 27 Aug 2015, 16:38

29 Aug 2015, 13:46

Hello

I have the next problem, i have a page with view scope.

I have a datatable with rows as entity. Each entity has an image and if you want to edit, you can do it through p:dialog.

So i load the data of the entity with a commandButton (with actionListener) and open the dialog (with oncomplete) too:

Code: Select all

<p:commandLink id="btnImgNtrDlg" update=":dlgImgNtr"
                        actionListener="#{entrevistaBean
                                          .getEntrevistaPk(obj.codEntrevista)}"
                        oncomplete="mostrarComponente('wdgImgNtr')">
            <h:graphicImage value="#{msj.imgCamara}" styleClass="imgCentro"/>
         </p:commandLink>
In dialog:

Code: Select all

<p:graphicImage id="ghLoadImg" cache="false" 
                               styleClass="margin10 heigth70"
                               alt="#{entrevistaBean.auxNtr.problemaPrincipal}"
                               value="#{entrevistaBean.auxNtr.imagen}"
                               rendered="#{entrevistaBean
                                          .isNtrImagen(entrevistaBean.auxNtr)}">
                  <f:viewParam name="paramId"  
                              value="#{entrevistaBean.auxNtr.codEntrevista}" />
               </p:graphicImage>
In backingBean we try to load the image:

Code: Select all

public StreamedContent getImagen() {
      FacesContext context;
      context = FacesContext.getCurrentInstance();
 
      // Devolvemos StreamedContent real con la imagen en bytes.     
      EntrevistaBean bean;
      String paramId;
      paramId = context.getExternalContext().getRequestParameterMap().get("paramId");
      if (paramId != null) {
         bean = entrevistaSrv.getEntrevistaId(Integer.valueOf(paramId));
         return new DefaultStreamedContent(
                 new ByteArrayInputStream(bean.getImgEntrevista()));
      } else {
         return new DefaultStreamedContent(
                 new ByteArrayInputStream(this.getImgEntrevista()));
      }  
   }
But graphicImage never load correctly. I was reading about set the id of graphicImage and i think could be the problem. I had testing several ways to set the imageId but I did not get it.


If I change the scope to session (and a few changes in backing bean) the image is displayed it but i want to get it with view scope, is it possible?

Help!
Primefaces 5.0
Hibernate 4.2.7
SpringFramework 4.0.1

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

29 Aug 2015, 17:36

StreamedContent doesn't work with ViewScoped because the image is streamed in another request.
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

AFC_Primefaces
Posts: 4
Joined: 27 Aug 2015, 16:38

29 Aug 2015, 18:34

So there is no way to load images from database with view scope?

Thanks for your reply, tandraschko
Primefaces 5.0
Hibernate 4.2.7
SpringFramework 4.0.1

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

30 Aug 2015, 10:38

exactly. But as you already pass a param to the resource request, you can just pass the id of the entity and load it in this request.
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 30 guests