简体   繁体   English

如何使用 <p:graphicImage> 在ui:repeat中使用DefaultStreamedContent?

[英]How to use <p:graphicImage> with DefaultStreamedContent in an ui:repeat?

I was trying to display a panel where user can see a list of items category(displayed as images) and on clicking they can view products within the category(images will be displayed) 我试图显示一个面板,用户可以在其中查看商品类别的列表(显示为图像),然后单击它们可以查看类别中的产品(将显示图像)

For displaying the item category, i used the ui:repeat nad the supporting bean calss Below is my xhtml code 为了显示项目类别,我使用了ui:repeat和支持bean calss。这是我的xhtml代码

<ui:repeat id="repeat" value="#{getData.images}" var="img" varStatus="loop">
<h:panelGroup>
<p:graphicImage id="img1" value="#{img}" alt="image not available" >
</p:graphicImage>
</h:panelGroup>
</ui:repeat>

And the Managed Bean Code parts 以及托管Bean代码部分

private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private List<StreamedContent> imageList = new ArrayList<StreamedContent>();

public List<StreamedContent> getImages(){
  for (int i = 0; i < sdh.getNumOfImages(); i++) {
    imageID = imageIDArray.get(i);
    ImageService imgSer = new ImageService();
    imgList.add(imageID);
    imgSer.setData(imageID);
    baos = imgSer.getImage();
    try {
      imageList.add(new DefaultStreamedContent(new 
            ByteArrayInputStream(baos.toByteArray())));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
  }
  imageNum = 0;
  return imageList;
}

public StreamedContent getData() {
    baos = imageList.get(imageNum);
    //imageList.add(baos);
    imageNum++;
    return new DefaultStreamedContent(new ByteArrayInputStream(baos.toByteArray()));
}

Now my problem if i don't uncomment the 'imageList.add(baos)' in 'getData', the images are not displayed. 现在我的问题是,如果我不取消注释“ getData”中的“ imageList.add(baos)”,则不会显示图像。 Now i really wants to know how the 'ui:repeat' works, since the 'imageList' contains the images and i can save the same if required in either of the method. 现在,我真的很想知道'ui:repeat'是如何工作的,因为'imageList'包含图像,并且我可以在任何一种方法中都保存相同的图像。 If i specify a fixed number (ex:'imageList.get(0)') in the 'getData' method then the same image is show multiple times. 如果我在'getData'方法中指定了固定数字(例如:'imageList.get(0)'),则同一图像将被多次显示。 Where as if i put the 'imageNum' without the 'imageList.add(baos)' it throw error 'Error in streaming dynamic resource' 好像我把'imageNum'没有'imageList.add(baos)'一样抛出了错误'流动态资源错误'

I tired Bjorn Pollex's suggestion and made the necessary changes but now images don't appear 我厌倦了Bjorn Pollex的建议,并进行了必要的更改,但现在图像没有出现

It is not possible to use <p:graphicImage> this way. 这样无法使用<p:graphicImage> You should rather iterate over a collection of unique image identifiers, not over a collection of StreamedContent . 您应该对唯一的图像标识符集合进行迭代,而不是对StreamedContent集合进行迭代。 Those unique image identifiers have then to be passed as a <f:param> to <p:graphicImage> which in turn will generate the right URLs for the browser. 然后,这些唯一的图像标识符必须作为<f:param>传递给<p:graphicImage> ,这将反过来为浏览器生成正确的URL。

<ui:repeat value="#{data.imageIds}" var="imageId">
    <p:graphicImage value="#{imageStreamer.image}">
        <f:param name="id" value="#{imageId}" />
    </p:graphicImage>
</ui:repeat>

Your #{data} managed bean must just have a: 您的#{data}托管Bean必须仅具有:

private List<Long> imageIds; // +getter

The #{imageStreamer} should be a separate application scoped managed bean which look basically like this: #{imageStreamer}应该是一个单独的应用程序范围的托管Bean,基本上看起来像这样:

@ManagedBean
@ApplicationScoped
public class ImageStreamer {

    @EJB
    private ImageService service;

    public StreamedContent getImage() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
            return new DefaultStreamedContent();
        }
        else {
            // So, browser is requesting the image. Get ID value from actual request param.
            String id = context.getExternalContext().getRequestParameterMap().get("id");
            Image image = service.find(Long.valueOf(id));
            return new DefaultStreamedContent(new ByteArrayInputStream(image.getBytes()));
        }
    }

}

You used wrong ui:repeat tag. 您使用了错误的ui:repeat标记。 You have var attribute but you can't use this in p:graphicImage tag value attribute.Please see sample usage, 您具有var属性,但不能在p:graphicImage标签值属性中使用此属性。请参阅示例用法,

       <ui:repeat value="#{yourBean.images}" var="img">
           <p:graphicImage value="/images/#{img}" />
        </ui:repeat>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM