gpt4 book ai didi

jsf-2 - 使用 p :graphicImage in ui:repeat or p:dataTable

转载 作者:行者123 更新时间:2023-12-01 11:03:55 29 4
gpt4 key购买 nike

我有一个 Bean,它有一个对象列表,其中包含表示数据库中图像的 StreamedContent 对象(Primefaces 类型)。现在我想在 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>

图像加载器 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;
}

}

我没有看到问题,你能帮我解决一下吗? :)

感谢和问候

最佳答案

我为此花了一天左右的时间,但我找到了解决方案!

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

关于jsf-2 - 使用 p :graphicImage in ui:repeat or p:dataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8042517/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com