【问题标题】:How to show images in p:dataGrid using p:graphicImage如何使用 p:graphicImage 在 p:dataGrid 中显示图像
【发布时间】:2012-02-06 21:36:22
【问题描述】:

我有一个 p:dataGrid 显示公司。这些公司有标志,我想展示他们的标志
在其名称下的 dataGrid 中,但图像未出现在 page 中。只出现了一个破碎的图像方块。

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() 显示登录人的个人资料图片并且不带参数效果很好! .我不明白为什么公司的徽标没有出现在页面中:/

任何人都可以帮助我解决这种情况或建议另一种显示图像的方式吗?

提前致谢。

【问题讨论】:

  • 问题看起来很像question
  • 无论如何而不是返回字节流。有更好的方法可以做到这一点,请参阅question/answer
  • 谢谢,但是我需要从 db 获取动态图像,并且我没有问题将参数发送到方法,因为第一个代码需要参数并且可以工作,但只有我认为 p:graphicImage 需要一个 getter

标签: java jsf jsf-2 primefaces


【解决方案1】:

为 graphicsImage 值创建一个字符串 url (/images/company.png),而不是 DefaultStreamedContent。编写一个 servlet,添加到 web.xml 并映射您的图像 url (/images/*)。

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

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

【讨论】:

  • 谢谢!我还使用 ImageIO 来编写图像响应输出流,现在它可以工作了 :)
猜你喜欢
  • 2011-10-23
  • 2012-06-16
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 2016-12-13
相关资源
最近更新 更多