在ui:repeat或p:dataTable中使用p:graphicImage

时间:2011-11-07 20:58:12

标签: jsf-2 primefaces iteration

我有一个Bean,它有一个对象列表,包含表示数据库中图像的StreamedContent对象(Primefaces Type)。 现在我想在JSF 2.0页面(使用Primefaces)中遍历此列表,并显示图像。 以这种方式只显示一个图像:

<p:graphicImage value="#{ImageLoader.oneImage}" title="aImage" alt="no Img"/>

但是,如果我将此标记嵌套在c:foreach,ui:repeat或p:datatable中,则不会加载图像!替代文本显示为

<ui:repeat value="#{ImageLoader.allImages}" var="img">
<p:graphicImage value="#{img}" alt="Hier sollte ein Bild sein" />
<br/>
</ui:repeat>

Image-Loader Bean:

@Named(value = "ImageLoader")
@Stateless
public class ImageLoader {

@Inject
private ImageFacade imgFacade;

private List<StreamedContent> allImages = new ArrayList <>();

private StreamedContent oneImage;

public StreamedContent getOneImage() {
    List<Image> findAll = imgFacade.findAll();
    byte[] imagedata = findAll.get(0).getImagedata();
    StreamedContent retVal = new DefaultStreamedContent(new     ByteArrayInputStream(imagedata));
    return retVal;
}

public void setOneImage(StreamedContent oneImage) {
    this.oneImage = oneImage;
}

public List<StreamedContent> getAllImages() {
    List<Image> findAll = imgFacade.findAll();
    Logger.getLogger(ImageLoader.class.getName()).log(Level.INFO, "### COUNT: {0}", findAll.size());
    for (Image image : findAll) {
        byte[] imagedata = image.getImagedata();
        allImages.add(new DefaultStreamedContent(new ByteArrayInputStream(imagedata)));
        Logger.getLogger(ImageLoader.class.getName()).log(Level.INFO, "### Added Image {0}", image.getImagename());
    }
    return allImages;
}

public void setAllImages(ArrayList<StreamedContent> allImages) {
    this.allImages = allImages;
}

}

我没有看到问题,你能帮帮我吗? :)

谢谢&amp;此致

1 个答案:

答案 0 :(得分:6)

我在这个问题上打破了一天左右,但我找到了一个解决方案!

JSF:

<ui:repeat value="#{bean.images}" var="imageID">
             <p:graphicImage value="#{bean.image}">
                 <f:param name="imageID" value="#{imageID}" />
             </p:graphicImage>
 </ui:repeat>

管理bean:

public List<String> getImages(){
        List<String> l = new ArrayList<String>();
        for(Theme t:themeFacade.findAll())
            l.add(t.getId().toString());
        return l;
    }

    public StreamedContent getImage(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest myRequest = (HttpServletRequest) context.getExternalContext().getRequest();
        String imageID = (String) myRequest.getParameter("imageID");
        return new DefaultStreamedContent(new ByteArrayInputStream(themeFacade.find(Long.parseLong(imageID)).getImage()));
    }