0

我在数据网格列中显示图像时遇到问题。当 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>
4

1 回答 1

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

于 2013-01-17T15:12:43.050 回答