使用primefaces加载一组图像

时间:2010-05-25 14:54:53

标签: java jsf primefaces

我有下一个代码来加载一组图像,这些图像的流位于名为names的数据模型中。我的问题是当我在p:datatable标签中声明var时似乎什么都没有。任何的想法? THX!

<p:dataTable value="#{controlador.names}"  var="nombre" rendered="true">
                <p:column rendered="true">
                    <h:outputText value="#{nombre.stream}"/>
                    <p:graphicImage value="#{nombre.stream}"/>
                </p:column>
            </p:dataTable>

3 个答案:

答案 0 :(得分:1)

p:graphicImage使用另一个请求,因此您需要像这样将标识符传递给managedBean。

<p:dataTable value="#{productManaged.products}" var="productIterated">
     <p:column>
          <f:facet name="header">
               <h:outputText value="#{product.pic}"/>
          </f:facet>
          <p:graphicImage value="#{productManaged.dynamicProductImage}">
               <f:param name="product_id" value="#{productIterated.id}"/>
          </p:graphicImage>
     </p:column>
</p:dataTable>

你应该注意的另一件事是在StreamedContent中返回一些东西或者会失败。做这样的事情:

public StreamedContent getDynamicProductImage() {
       String id = FacesContext.getCurrentInstance()
                       .getExternalContext().getRequestParameterMap().get("product_id");
       if(id!=null && this.products!=null && !this.products.isEmpty()){
           Integer productId = Integer.parseInt(id);
           for(Product productTemp:this.products){
               if(productTemp.getId().equals(productId)){
                   return new DefaultStreamedContent(
                        new ByteArrayInputStream(productTemp.getImage()),                            
                             productTemp.getMimeType());
               }
           }
       }
       return new DefaultStreamedContent(
                        new ByteArrayInputStream(this.products.get(0).getImage()), 
                             this.products.get(0).getMimeType()); //if you return null here then it won't work!!! You have to return something.
}

或者您可以阅读此帖子http://primefaces.prime.com.tr/forum/viewtopic.php?f=3&t=4163

答案 1 :(得分:1)

在浪费了几个小时的时间来实现我为这个问题找到的许多解决方案的过程(即包括一个参数或属性),我设法找到的唯一有效的解决方案可以在这里找到:Serving Dynamic Content with PrettyFaces

答案 2 :(得分:0)

删除<h:outputText>。您只能阅读一次的信息流。它不能再读一次了。

对于p:graphicImage部分,您需要为其提供值DefaultStreamedContent。另请参阅this blog entry