【问题标题】:Graphic Image not showing in Datagrid PrimeFaces图形图像未显示在 Datagrid PrimeFaces 中
【发布时间】:2013-01-17 05:24:53
【问题描述】:

我在数据网格列中显示图像时遇到问题。当 Datagrid 加载时,它会正确显示图像,但是每当我单击第二页或刷新第一页时,图像就消失了。我在控制台中注意到,单击第二页或刷新页面使参数值为空。这就是为什么图像没有显示。我正在使用会话范围。下面是我的代码:

 public StreamedContent getStreamedImageById() {
FacesContext context = FacesContext.getCurrentInstance();

if (context.getRenderResponse()) {
    // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.

    System.out.println("check");
    return new DefaultStreamedContent();
}
else {
    // So, browser is requesting the image. Get ID value from actual request param.
    String firstName = context.getExternalContext().getRequestParameterMap().get("firstName");
    System.out.println("Name:"+firstName);
    System.out.println("Image::"+images.get(firstName));

    return images.get(firstName);

}

在搜索方法中,我只取散列图中的所有图像。

      while(itr.hasNext()){

                com.sysvana.request.UserBean us=itr.next();

            images.put(us.getFirstName(), stringToStreamedContent(us.getJpegPhoto()));
            }

这是我的 xhtml::

         <p:graphicImage value="#{userManagementActionBean.streamedImageById}" height="40" width="50" style="align:center"  >

                <f:param id="firstName" name="firstName" value="#{user.firstName}" />
              </p:graphicImage>

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    这个,

    return images.get(firstName);
    

    不对。您应该在此处创建流式内容,而不是返回之前在之前的 HTTP 请求中创建的内容。重点是您不应该在之前的 HTTP 请求中创建它,而应直接在调用 getStreamedImageById() 方法的同一 HTTP 请求中创建它。

    如下修复它(假设userBean.getJpegPhoto() 返回byte[]

    UserBean userBean = userService.findByFirstName(firstName);
    return new DefaultStreamedContent(new ByteArrayInputStream(userBean.getJpegPhoto())); 
    

    另见:


    与具体问题无关,方法名称stringToStreamedContent() 暗示了一个主要问题。图像绝对不能表示为String,而是表示为byte[]。将图像视为String 时,由于字符编码问题导致信息丢失,您最终可能会出现损坏的图像。您应该从数据库中以byte[]InputStream 的形式获取图像。

    【讨论】:

    • 感谢您的建议。但我的问题与获取参数值有关。每当我点击datagrid分页或刷新页面时,我的意思是第二次调用getStreamedImageById()方法,param为null。
    • 好的.. 现在我成功地在网格中显示了图像。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-07-20
    • 2021-08-26
    • 2012-07-28
    • 1970-01-01
    • 2012-03-20
    • 2014-07-25
    • 1970-01-01
    • 2015-10-30
    相关资源
    最近更新 更多