0

I have a p:dataGrid which shows companies . These companies have logo and I want to show their logo
in dataGrid under of their names but images don't appear in page . Only appears a broken image square .

The xhtml code ;

 <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>

The managed bean code ;

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

When I change code like that , I take an exception "Method companyLogo not found"

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

But another page I used getPersonPicture() which shows loged person's profile picture and doesn't take parameter works great ! . I don't understand why companies' logos don't appear in page :/

Can anyone help me about this situation or suggest another way to show images ?

Thanks in advance .

Jman
  • 481
  • 1
  • 4
  • 16
  • the problem looks similar [question](http://stackoverflow.com/questions/9083491/how-to-pass-method-arguments-to-an-actionlistener) – Ravi Kadaboina Feb 06 '12 at 23:28
  • anyways instead of returning a byte stream. There is a better way to do this, see this [question/answer](http://stackoverflow.com/questions/7975089/nesting-of-el-expressions-in-jsf-for-resource-api) – Ravi Kadaboina Feb 06 '12 at 23:32
  • thanks , but I need to get images from db as dynamic and I don't have problem sending parameter to method because first code takes parameter and works but only I thought that p:graphicImage need a getter – Jman Feb 07 '12 at 02:48

1 Answers1

2

Create a String url (/images/company.png) instaed of DefaultStreamedContent for graphicImage value. Write a servlet, add to web.xml and map your image url (/images/*).

Do anything with database in servlet doGet and return your image stream as response.

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

besc
  • 481
  • 3
  • 8
  • thanks ! I used also ImageIO to write image response outputstream and now it works :) – Jman Feb 07 '12 at 13:52