1

I have been trying to show some images depending on a String parameter.

I´m using java, Jboss, JSF and primefaces. The images are here: ...Desktop\wildfly-10.0.0\bin\img\ and I use the route: To save images when I load new restaurants to the application To load the images when a client is showing restaurants.

The code I´m using:

ManagedBean

package com.activity.presentation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.UploadedFile;

public class ImageLoaderBean implements Serializable {

    private static final long serialVersionUID = 1L;

    // Cargador de imagenes de un restaurante
    private UploadedFile image;
    private DefaultStreamedContent imageLoad;
    private Map<String, DefaultStreamedContent> images;


    public ImageLoaderBean(){
        images = new HashMap<>();
    }
    ////////////////////////////////////////////////////////////////////////////
    // Getters and setters
    ////////////////////////////////////////////////////////////////////////////

    public UploadedFile getImage() {
        return image;
    }

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

    public DefaultStreamedContent getImageLoad() {
        return imageLoad;
    }

    public void setImageLoad(DefaultStreamedContent imageLoad) {
        this.imageLoad = imageLoad;
    }

    public void saveImage() throws IOException {
        String imageName = image.getFileName();
        InputStream input = image.getInputstream();
        String path = new File("").getAbsolutePath() + "//img";
        OutputStream output = new FileOutputStream(new File(path, imageName));

        try {
            IOUtils.copy(input, output);
        } finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
        }
    }

    public DefaultStreamedContent loadImage(String name) throws FileNotFoundException {
        if(images.containsKey(name))
            return images.get(name);
        else{
            String path = new File("").getAbsolutePath() + "//img//" + name + ".jpg";
            FileInputStream fs = new FileInputStream(path);
            imageLoad = new DefaultStreamedContent(fs, "image/jpeg");

            images.put(name, imageLoad);

            return imageLoad;
        }
    }

}

The xhtml page:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    template="/templates/template-user.xhtml">

    <ui:define name="body">
        <div class="container_24">
            <p:dataScroller value="#{actividades.restaurantes}" var="restaurante"
                chunkSize="6">
                <f:facet name="header">
                    Use scroll to show more restaurants
                </f:facet>

                <h:panelGrid columns="2">
                    <p:outputPanel>
                        <p:graphicImage value="#{imageLoader.loadImage(restaurante.name)}"
                            styleClass="imagen" />
                    </p:outputPanel>

                    <p:outputPanel>
                        <h:panelGrid columns="2" cellpadding="5">
                            <h:outputText value="Id:" />
                            <h:outputText value="#{restaurante.id}" styleClass="negrita" />

                            <h:outputText value="Nombre:" />
                            <h:outputText value="#{restaurante.name}" styleClass="negrita" />

                            <h:outputText value="Teléfono:" />
                            <h:outputText value="#{restaurante.telephone}"
                                styleClass="negrita" />

                            <h:outputText value="Dirección:" />
                            <h:outputText value="#{restaurante.address}" styleClass="negrita" />
                        </h:panelGrid>
                    </p:outputPanel>
                </h:panelGrid>
            </p:dataScroller>
        </div>
    </ui:define>
</ui:composition>

The method loadImage is working fine when I debbug it (adding different images with the right name and path to the images map), but when I see the page on the webbrowser I always see the same image. I use the "inspect element" option to see the details and images always have same src on the webbrowser:

img id="j_idt23:j_idt25:0:j_idt29"     src="/recomendador/javax.faces.resource/dynamiccontent.properties.xhtml?    ln=primefaces&amp;v=5.3&amp;pfdrid=D6DixI6zLgdnWFwfifCqRCLll30J5oCGmp92SYOEGh1piAq3Q75ekynDCGWApZML&amp;pfdrt=sc&amp;pfdrid_c=true" alt="" class="imagen"

I dont know why it happens when the bean method is returning the right value.

ManagedBeans used in the code have session scope. I used all scopes and I used some attributes: cache="false", changing the value for name attribute and I couldnt solve the problem. Thanks a lot

  • Not sure as to PrimeFaces behavior (related question here: http://stackoverflow.com/q/36109873), but the following links should certainly be helpful as you're going totally the wrong path as to referencing image location (depending on CWD in a Java EE application is an absolute no-no): http://stackoverflow.com/q/2308188, http://stackoverflow.com/q/18664579, http://stackoverflow.com/q/28081916 and http://stackoverflow.com/q/4543936 – BalusC Apr 03 '16 at 12:03
  • @BalusC I solved it reading your answer at this post: [link]( stackoverflow.com/q/36109873). I just used omnifaces in case of primefaces, and i changed the loadImage method to return InputStream in case of DefaultStreamedContent. Really thank you! – Cristian González Fernández Apr 03 '16 at 15:35

0 Answers0