<p:graphicImage>的使用方法在 ui:repeat 中使用 DefaultStreamedContent? [英] How to use &lt;p:graphicImage&gt; with DefaultStreamedContent in an ui:repeat?

查看:23
本文介绍了<p:graphicImage>的使用方法在 ui:repeat 中使用 DefaultStreamedContent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示一个面板,用户可以在其中查看项目类别列表(显示为图像),点击后他们可以查看类别中的产品(将显示图像)

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)

为了显示项目类别,我使用了 ui:repeat nad 支持 bean calss下面是我的 xhtml 代码

For displaying the item category, i used the ui:repeat nad the supporting bean calss Below is my xhtml code

<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>

和托管 Bean 代码部分

And the Managed Bean Code parts

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()));
}

现在我的问题是,如果我不取消对getData"中的imageList.add(baos)"的注释,图像将不会显示.现在我真的很想知道 'ui:repeat' 是如何工作的,因为 'imageList' 包含图像,如果需要,我可以在任何一种方法中保存相同的图像.如果我在 'getData' 方法中指定了一个固定的数字(例如:'imageList.get(0)'),那么同一张图片会显示多次.好像我把 'imageNum' 放在没有 'imageList.add(baos)' 的地方它会抛出错误 'Error in streaming dynamic resource'

Now my problem if i don't uncomment the 'imageList.add(baos)' in 'getData', the images are not displayed. 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. If i specify a fixed number (ex:'imageList.get(0)') in the 'getData' method then the same image is show multiple times. Where as if i put the 'imageNum' without the 'imageList.add(baos)' it throw error 'Error in streaming dynamic resource'

我厌倦了 Bjorn Pollex 的建议并进行了必要的更改,但现在图像没有出现

I tired Bjorn Pollex's suggestion and made the necessary changes but now images don't appear

推荐答案

不可能以这种方式使用 .您应该遍历唯一图像标识符的集合,而不是遍历 StreamedContent 的集合.然后,这些唯一的图像标识符必须作为 传递给 ,后者又会为浏览器生成正确的 URL.

It is not possible to use <p:graphicImage> this way. You should rather iterate over a collection of unique image identifiers, not over a collection of 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.

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

你的 #{data} 托管 bean 必须只有一个:

Your #{data} managed bean must just have a:

private List<Long> imageIds; // +getter

#{imageStreamer} 应该是一个单独的应用程序范围的托管 bean,它看起来基本上像这样:

The #{imageStreamer} should be a separate application scoped managed bean which look basically like this:

@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()));
        }
    }

}

这篇关于<p:graphicImage>的使用方法在 ui:repeat 中使用 DefaultStreamedContent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆