PrimeFaces Datatable Pagination中的动态图像

时间:2013-10-31 12:57:11

标签: jsf-2 primefaces

在BalusC博客的帮助下,我成功地在Primeface数据表中实现了动态图像渲染。

现在我的问题是:数据来自数据表的第一页但是如果我使用分页数据移动到第二页则会丢失。即使我在第一页再回头也没有什么事情发生。

这是我的代码:

     <p:dataTable id="users" var="user" styleClass="userSearchTable"
                    paginator="true" rows="5" rowKey="#{user}"
                    paginatorPosition="bottom"
                    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                    rowsPerPageTemplate="5,10,15"
                    rendered="#{userManagementBean.renderSearchResultPanel}"
                    value="#{userManagementBean.userList}" 
                    emptyMessage="No User Found!!"
                    selection="#{userManagementBean.user}" selectionMode="single">
                    <p:ajax event="rowSelect" update=":form,:form2"
                        onstart="userSelect.show()" oncomplete="userSelect.hide()"
                        listener="#{userManagementActionBean.viewModifyProfile()}" />

                    <p:column style="text-align:center">

                        <p:graphicImage value="#{imageUploadBean.streamedImageById}"
                            height="50" width="50">

                            <f:param id="bean" name="bean" value="#{user}" />

                        </p:graphicImage>
        ............

Bean类:

     public StreamedContent getStreamedImageById()
    throws IOException {

    FacesContext context = FacesContext.getCurrentInstance();
    if (context.getRenderResponse()) {

        System.out.println("check");
        return new DefaultStreamedContent();
    }
    else {
        System.out.println("down");
        UserBean userBean =
            findByUserBean(context.getExternalContext().getRequestParameterMap().get(
                "bean"));

        return stringToStreamedContent(userBean.getJpegPhoto());

    }
}


      public StreamedContent stringToStreamedContent(String recv)
    throws IOException {

    try {
        byte[] imageByteArray = decodeImage(recv);
        InputStream is = null;

        is = new ByteArrayInputStream(imageByteArray);

        image =
            new DefaultStreamedContent(
                is, "image/jpeg");
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return image;

}

1 个答案:

答案 0 :(得分:0)

我得到了解决方案。如果任何一个人面临这个问题可以得到帮助: 它应该是

 if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {

        return new DefaultStreamedContent();
    }

而不是

 if (context.getRenderResponse()) {

     return new DefaultStreamedContent();
  }