简体   繁体   English

使用分页在数据表中的Primefaces动态图像

[英]Primefaces dynamic image in datatable using pagination

I have successfully implemented the solution suggested by BalusC for rendering dynamic images inside a datatable (code posted further down here). 我已经成功实现了BalusC建议的解决方案,用于在数据表中渲染动态图像(代码在此处发布)。 Problem I am facing is this: 我面临的问题是:

Issue 问题

I am using pagination in datatable and when I move to the page which I have never visited, the images render fine. 我在数据表中使用分页 ,当我移至从未访问过的页面时,图像呈现良好。 But when I come back to the page I had visited earlier, the images don't show up even though the StreamedContent is returned for that image. 但是,当我回到之前访问的页面时,即使为该图像返回了StreamedContent,这些图像也不会显示。

Code

// ImageBean - SessionScoped        
// Get image for compound
public StreamedContent scaledImageById() {
    FacesContext context = FacesContext.getCurrentInstance();

    if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
        return new DefaultStreamedContent();
    } else {
        String idStr = context.getExternalContext().
                                       getRequestParameterMap().get("id");
        StreamedContent image = scaledCompoundImageMap.
                                              get(Integer.valueOf(idStr));
        return image;
    }
}


// Controller - ViewScoped
<p:dataTable id="cTable" value="#{controller.compoundList}" var="compound">
       <p:column headerText="Structure" style="text-align:center">
          <p:graphicImage id="scaledImage" 
                          value="#{imageBean.scaledImageById()}"     
                          cache="false">
                <f:param name="id" value="#{compound.id}" />
          </p:graphicImage>
       </p:column>   
  ...

So when I visit a new page in the paginator, the images display fine. 因此,当我在分页器中访问新页面时,图像显示良好。 But when I come back to an already visited page, the images are not shown (even though the scaledImageById is called twice and the StreamedContent is returned fine). 但是当我回到已经访问过的页面时,图像不会显示(即使scaledImageById被调用了两次并且StreamedContent返回的很好)。

Please let me know if you need any other code here and any help would be much appreciated. 如果您在这里需要任何其他代码,请告诉我,我们将不胜感激。 Thanks. 谢谢。

Abdul 阿卜杜勒

From the HTML side, you have img element: 在HTML方面,您具有img元素:

<img src="some_url?id=xyz"/>

If the images are stored in the database, and are never changed (uploading new image creates new image id in your entity), you can achieve more flexibility and performance creating custom servlet for images. 如果图像存储在数据库中并且从未更改(上载新图像会在您的实体中创建新的图像ID),则可以为图像创建自定义servlet,从而获得更大的灵活性和性能。

1) Create the servlet and bind it to 'some_url' 1)创建servlet并将其绑定到“ some_url”

2) In the servlet, read the image from database (or anything else) 2)在servlet中,从数据库(或其他任何东西)读取图像

3) Set the headers, most important content type and content length, and cache headers 3)设置标头,最重要的内容类型和内容长度以及缓存标头

4) Write the content to the output stream 4)将内容写入输出流

The plus is, if you have constant url for the image and good caching policy, the browser would fetch the image from the cache - the bean doesn't have to be called each time. 加号的是,如果您具有恒定的图像URL和良好的缓存策略,则浏览器将从缓存中获取图像-不必每次都调用Bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM