Jsf 如何使用<;p:graphicImage>;在ui中使用DefaultStreamedContent:重复?
我试图显示一个面板,用户可以在其中看到一个项目类别列表(显示为图像),单击可以查看类别内的产品(将显示图像) 为了显示项目类别,我使用了ui:repeat和支持的bean cals 下面是我的xhtml代码Jsf 如何使用<;p:graphicImage>;在ui中使用DefaultStreamedContent:重复?,jsf,primefaces,uirepeat,graphicimage,Jsf,Primefaces,Uirepeat,Graphicimage,我试图显示一个面板,用户可以在其中看到一个项目类别列表(显示为图像),单击可以查看类别内的产品(将显示图像) 为了显示项目类别,我使用了ui:repeat和支持的bean cals 下面是我的xhtml代码 <ui:repeat id="repeat" value="#{getData.images}" var="img" varStatus="loop"> <h:panelGroup> <p:graphicImage id="img1" value="#{img}
<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()));
}
private ByteArrayOutputStream baos=new ByteArrayOutputStream();
私有列表imageList=新的ArrayList();
公共列表getImages(){
对于(int i=0;i
现在我的问题是,如果我不取消“getData”中“imageList.add(baos)”的注释,则不会显示图像。
现在我真的想知道“ui:repeat”是如何工作的,因为“imageList”包含图像,如果需要,我可以在任何一种方法中保存相同的图像。如果我在“getData”方法中指定一个固定的数字(例如:“imageList.get(0)”),那么同一个图像将显示多次。其中,我将“imageNum”放在没有“imageList.add(baos)”的地方,“它抛出错误”“流动态资源中的错误”
我厌倦了Bjorn Pollex的建议,并做了必要的更改,但现在图像没有出现您使用了错误的ui:repeat标记。您有var属性,但不能在p:graphicImage标记值属性中使用它。请参阅示例用法
<ui:repeat value="#{yourBean.images}" var="img">
<p:graphicImage value="/images/#{img}" />
</ui:repeat>
不可能以这种方式使用
。您应该迭代唯一图像标识符的集合,而不是StreamedContent
的集合。然后,这些唯一的图像标识符必须作为
传递给
,从而为浏览器生成正确的URL
<ui:repeat value="#{data.imageIds}" var="imageId">
<p:graphicImage value="#{imageStreamer.image}">
<f:param name="id" value="#{imageId}" />
</p:graphicImage>
</ui:repeat>
#{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()));
}
}
}
您没有在
中引用循环变量(img
)。你的getImages
应该返回一个List
,你的
应该有value=“#{img}”
。Hankyu BalusC它工作了,但是我必须注释@EJB,如果我没有,我会得到“com.sun.faces.mgbean.ManagedBeanCreationException:在托管bean imageStreamer上执行资源注入时出错”我还有一个问题>当我刷新页面时,图像会消失,为什么会这样。再次感谢,不客气。嗯,我只是假设您正在使用EJB进行业务/数据服务。它提供了一种透明地处理DB事务的简单而安全的方法。如果你没有使用它,那么就按照你通常的方式来获得服务。还有一个问题,如果我需要在图像上获得点击事件,那么我该怎么做。我无法将Ajax放入ui中:重复。@BalusC如果找不到图像,如何返回404错误?
@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()));
}
}
}