2012-02-06 35 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