如何使用< 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)
为了显示项目类别,我使用了ui:repeat和支持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)'一样,它抛出错误'流动态资源错误'
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
推荐答案
无法通过这种方式使用<p:graphicImage>
.您应该遍历唯一的图像标识符的集合,而不是遍历StreamedContent
的集合.然后,这些唯一的图像标识符必须作为<f:param>
传递给<p:graphicImage>
,这反过来将为浏览器生成正确的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屋!