如何在ui中使用p:graphicImage和StreamedContent:repeat / h:dataTable / p:dataTable? [英] How to use p:graphicImage with StreamedContent inside ui:repeat/h:dataTable/p:dataTable?

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

问题描述

我有一个带有 StreamedContent 属性的对象列表,我需要在< p:graphicImage> 组件。

I have a list of objects with a StreamedContent property and I need to show it in the <p:graphicImage> component.

<ui:repeat value="#{testuser.answers}" var="imageanswers">
    <p:graphicImage value="#{imageanswers.imageAnswer}" alt="Image" width="200" height="100" />
    <br />
</ui:repeat>

但未显示。它显示alt文本。我在另一篇文章中看到了在ui中使用p:graphicImage:repeat或者p:dataTable ,但我不明白 getImages()方法。

But this isn't shown. It displays the alt text instead. I saw this other post Use of p:graphicImage in ui:repeat or p:dataTable, but I don't understand the getImages() method.

推荐答案


我没有得到getImages方法

I don´t get the getImages method

getImages method是 images 属性的getter,恰好设置为 的属性< ui:repeat>

The getImages method is the getter for the images property which happens to be set as the value attribute of the <ui:repeat>.

那么为什么它是一个String列表对象?我认为 graphicImage 组件的值必须是StreamedContent?

So why is it a list of String objects? I thought that the graphicImage component must have a value that is a StreamedContent?

是的,它的值必须是StreamedContent ,但你不能循环通过StreamedContents来构建页面上的图像列表。原因是 graphicImage Primefaces组件在HTML页面上呈现标准HTML < img> 标记。如果您要查看Firebug或Fiddler或其他工具的控制台输出,您会注意到JSF页面的请求,然后您还会注意到页面引用的每个图像资源的单独请求。这些图像资源必须从一系列单独的请求中逐个加载,那么ManagedBean如何知道哪个请求映射到哪个图像?

Yes its value must be a StreamedContent, but you can't loop through StreamedContents to build a list of images on the page. The reason why is that the graphicImage Primefaces component renders on the HTML page to a standard HTML <img> tag. If you were to look at the console output for Firebug or Fiddler or some other tool, you will notice the request for the JSF page, and then you will also notice a seperate request for each image resource the page references to. These image resources have to be loaded one by one from a series of seperate requests, so how is the ManagedBean supposed to know which request maps to which image?

这就是为什么repeat组件必须遍历一个唯一的图像id列表,因为从本质上讲,当它构建一个graphicImage组件时,它正在构建一个HTML图像标记,其中引用了一个特定图像的web服务器,由id标识。要发送此唯一ID,他们使用< f:param> facelets标记,该标记会添加标识为HTTP请求参数的唯一标识。

This is why the repeat component must iterate over a list of unique image ids because in essence when it builds a graphicImage component it is building an HTML image tag with a reference to the webserver for a specific image, identified by id. To send this unique id, they are using the <f:param> facelets tag which adds the unique identified as an HTTP request parameter.

<ui:repeat value="#{bean.images}" var="imageID">
   <p:graphicImage value="#{bean.image}">
      <f:param name="imageID" value="#{imageID}" />
   </p:graphicImage>
</ui:repeat>

重复<$的属性c $ c> graphicImage 是一个托管bean属性getter,它与加载每隔一个 graphicImage 组件得到的图像的方法相同。区别在于,在 image 属性getter方法中,我们可以使用 FacesContext 来获取我们传递的唯一imageID HTTP请求参数。

The value attribute of the repeated graphicImage is a managed bean property getter and it is the same method for loading the image that every other graphicImage component gets. The difference is that in the image property getter method we can uses the FacesContext to get our unique imageID that was passed as an HTTP request parameter.

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

这篇关于如何在ui中使用p:graphicImage和StreamedContent:repeat / h:dataTable / p:dataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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