2012-02-06 46 views
0

我有一个显示公司的p:dataGrid。这些公司有徽标,我想在他们的名称下显示其数据网格中的徽标
,但图像不显示在页面中。只出现一个破碎的图像广场。如何在p:dataGrid中使用p显示图像:graphicImage

xhtml代码;

<p:dataGrid value="#{BDS_Company.companyList}" var="cmp" rows="5" columns="3"> 
     <p:column> 
      <h:panelGrid columns="1"> 
      <h:outputLabel value="#{cmp.cmpName}"/> 
      <h:outputLabel value="#{cmp.cmpEmail}"/> 
      <p:graphicImage value="#{BDS_Company.companyLogo(cmp)}"/> 
      </h:panelGrid> 
     </p:column> 
</p:dataGrid> 

托管的bean代码;

public DefaultStreamedContent companyLogo(BdsCompany company) { 
     if (company != null) { 
      if (company.getCmpLogo() != null) { 
       InputStream is = new ByteArrayInputStream(company.getCmpLogo().getDocFile()); 
       return new DefaultStreamedContent(is); 
      } 
     } 
     return null; 
    } 

当我改变这样的代码,我拿一个异常“方法CompanyLogo的未找到”

public DefaultStreamedContent getCompanyLogo(BdsCompany company) { 
     if (company != null) { 
      if (company.getCmpLogo() != null) { 
       InputStream is = new  ByteArrayInputStream(company.getCmpLogo().getDocFile()); 
       return new DefaultStreamedContent(is); 
      } 
     } 
     return null; 
} 

但我用getPersonPicture(品),显示loged人的个人资料图片,并没有考虑其他页面参数很好用! 。我不明白为什么公司的标识不会出现在页面中:/

任何人都可以帮助我了解这种情况,或者提出另一种显示图像的方式吗?

在此先感谢。

+0

问题看起来类似于[问题](http://stackoverflow.com/questions/9083491/how-to-pass-method-arguments-to-an-actionlistener) – Ravi 2012-02-06 23:28:32

+0

反正而不是返回一个字节流。有更好的方法来做到这一点,看到这[问题/答案](http://stackoverflow.com/questions/7975089/nesting-of-el-expressions-in-jsf-for-resource-api) – Ravi 2012-02-06 23:32:41

+0

谢谢,但我需要从数据库中获取图像为动态,我没有问题发送参数给方法,因为第一个代码需要参数和工作,但只有我认为p:graphicImage需要一个getter – Jman 2012-02-07 02:48:45

回答

2

为graphicImage值创建DefaultStreamedContent的String url(/images/company.png)。编写一个servlet,添加到web.xml并映射你的图片url(/ images/*)。

在servlet doGet中执行任何与数据库有关的操作,并返回图像流作为响应。

http://rajivas.wordpress.com/tag/writing-directly-to-response-stream-in-jsf/

+0

谢谢!我也用ImageIO写图像响应输出流,现在它工作:) – Jman 2012-02-07 13:52:02