我试图显示一个面板,用户可以在其中查看项目类别列表(显示为图像),并且单击后可以查看该类别中的产品(将显示图像)
为了显示项目类别,我使用了 ui:repeat nad 支持 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>
以及托管 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()));
}
现在我的问题是,如果我不取消注释“getData”中的“imageList.add(baos)”,则不会显示图像。 现在我真的想知道“ui:repeat”是如何工作的,因为“imageList”包含图像,如果需要在任何一种方法中我都可以保存相同的图像。如果我在“getData”方法中指定固定数字(例如:“imageList.get(0)”),则同一图像会显示多次。就好像我将“imageNum”放在没有“imageList.add(baos)”的情况下,它会抛出错误“流动态资源时出错”
我厌倦了 Bjorn Pollex 的建议并进行了必要的更改,但现在图像没有出现
最佳答案
无法使用 <p:graphicImage>
这边走。您应该迭代唯一图像标识符的集合,而不是 StreamedContent
的集合。 。然后,这些唯一的图像标识符必须作为 <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>
您的#{data}
托管 bean 必须只有:
private List<Long> imageIds; // +getter
#{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()));
}
}
}
关于jsf - 如何使用<p :graphicImage> with DefaultStreamedContent in an ui:repeat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10944673/