Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将p:graphicImage与p:dataTable中的StreamedContent一起使用?_Image_Jsf_Jsf 2_Datatable_Primefaces - Fatal编程技术网

如何将p:graphicImage与p:dataTable中的StreamedContent一起使用?

如何将p:graphicImage与p:dataTable中的StreamedContent一起使用?,image,jsf,jsf-2,datatable,primefaces,Image,Jsf,Jsf 2,Datatable,Primefaces,我想使用PrimeFaces数据表从数据库动态加载图像。代码如下所示,它基于: 多亏了巴卢斯,我才开始工作。我已经使用RequestScope、SessionScope或ApplicationScope来管理GetStreamDimageId。但是,在UI中,始终设置默认图像(对于空情况),而不是与每行对应的图像。新守则是: public StreamedContent streamedById(Long id) { DefaultStreamedContent image = null

我想使用PrimeFaces数据表从数据库动态加载图像。代码如下所示,它基于:

多亏了巴卢斯,我才开始工作。我已经使用RequestScope、SessionScope或ApplicationScope来管理GetStreamDimageId。但是,在UI中,始终设置默认图像(对于空情况),而不是与每行对应的图像。新守则是:

public StreamedContent streamedById(Long id) {
    DefaultStreamedContent image = null;

    System.out.println("[ID inventario]: " + id);

    List<Inventario> listInventarios = controladorRegistrarPedido.listInventarios();
    for (Inventario i : listInventarios) {
        if (i.getId().equals(id)) {
            byte[] foto = i.getProducto().getFoto();
            if (foto != null) {
                System.out.println("   [Foto]: " + foto);
                image = new DefaultStreamedContent(new ByteArrayInputStream(foto), "image/png");
                break;
            }
        }


    }
    if (image == null) {
        System.out.println("       [Image null]");
        byte[] foto = listInventarios.get(0).getProducto().getFoto();
        image = new DefaultStreamedContent(new ByteArrayInputStream(foto), "image/png");
    }

    System.out.println("   [Foto Streamed]: " + image);

    return image;

}
public StreamedContent streamedById(长id){
DefaultStreamedContentImage=null;
System.out.println(“[ID inventario]:”+ID);
List listInventarios=controladorRegistrarPedido.listInventarios();
for(Inventario i:listInventarios){
如果(i.getId().equals(id)){
字节[]foto=i.getProducto().getFoto();
如果(foto!=null){
System.out.println(“[Foto]:”+Foto);
image=newdefaultstreamdcontent(newbytearrayinputstream(foto),“image/png”);
打破
}
}
}
if(image==null){
System.out.println(“[Image null]”);
字节[]foto=listInventarios.get(0.getProducto().getFoto();
image=newdefaultstreamdcontent(newbytearrayinputstream(foto),“image/png”);
}
System.out.println(“[Foto Streamed]:”+图像);
返回图像;
}
将调用getter方法两次。第一次是
属性。如果只返回
newdefaultstreamedcontent()
,则它将在
src
属性中自动生成正确的URL。第二次是当浏览器真正请求图像时,这是您应该返回实际图像的时刻

因此,getter方法基本上应该如下所示:

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

    if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
        // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
        return new DefaultStreamedContent();
    }
    else {
        // So, browser is requesting the image. Get ID value from actual request param.
        String id = context.getExternalContext().getRequestParameterMap().get("id");
        Image image = service.find(Long.valueOf(id));
        return new DefaultStreamedContent(new ByteArrayInputStream(image.getBytes()));
    }
}

在PrimeFaces 3.2中,错误仍然存在。我用它来解决问题

<p:graphicImage value="#{company.charting}">
    <f:param id="a" name="a" value="#{cc.attrs.a}" />
    <f:param id="b" name="b" value="#{cc.attrs.b}" />
</p:graphicImage>
但即使这样,bean也会被调用2次。但是在第二个调用中,变量a+b被填充;-)


该死的bug

如果我们通过hibernate保存图像,它们将作为字节[]保存在数据库中。我上传了带有
BalusC的图片,回答非常好,质量非常好。我使用了第一个解决方案,它使用长参数,我从数据库中获取图像,但是没有在UI中呈现,我检查了Glassfish日志,并且存在一些异常,知道吗?(我添加到我的帖子中)我首先分享例外情况,因为它们通常已经是自己的完整答案。stacktrace的类型、消息和第一行通常是足够的信息。完成了,很好的建议,很高兴知道像你这样的人使stackoverflow成为一个伟大的wiki社区。坚持下去!为
p:graphicImage
内容提供服务的bean需要是请求范围或会话范围(如果它是无状态的,甚至是应用程序范围)。不支持视图范围。您需要将该方法从视图范围的bean拆分为适当范围内的单独bean。这还有一个额外的好处,就是它可以更好地在其他地方重用。是的!说得好,我想我差不多准备好了。现在不会抛出异常,但是不会渲染图像(但是,如果我在它工作的UI中的registrarPedidoController.GetStreamDimageById(1)中放入硬编码的1)(当然会带来相同的图像)。我看到我的日志,出现了一种奇怪的情况,它正在查找等于0的id(当我的数据库中没有该数据时);我应该管理此案例还是空图像?对于GetStreamDimageById方法中的这些案例,我应该返回什么?谢谢,这很有用。在一个请求中,参数为null,而在另一个请求中,参数已填充,因此仅使用一个属性就足够了。只需在开始时检查参数是否为null。
public StreamedContent streamedById(Long id) {
    DefaultStreamedContent image = null;

    System.out.println("[ID inventario]: " + id);

    List<Inventario> listInventarios = controladorRegistrarPedido.listInventarios();
    for (Inventario i : listInventarios) {
        if (i.getId().equals(id)) {
            byte[] foto = i.getProducto().getFoto();
            if (foto != null) {
                System.out.println("   [Foto]: " + foto);
                image = new DefaultStreamedContent(new ByteArrayInputStream(foto), "image/png");
                break;
            }
        }


    }
    if (image == null) {
        System.out.println("       [Image null]");
        byte[] foto = listInventarios.get(0).getProducto().getFoto();
        image = new DefaultStreamedContent(new ByteArrayInputStream(foto), "image/png");
    }

    System.out.println("   [Foto Streamed]: " + image);

    return image;

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

    if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
        // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
        return new DefaultStreamedContent();
    }
    else {
        // So, browser is requesting the image. Get ID value from actual request param.
        String id = context.getExternalContext().getRequestParameterMap().get("id");
        Image image = service.find(Long.valueOf(id));
        return new DefaultStreamedContent(new ByteArrayInputStream(image.getBytes()));
    }
}
<p:graphicImage value="#{company.charting}">
    <f:param id="a" name="a" value="#{cc.attrs.a}" />
    <f:param id="b" name="b" value="#{cc.attrs.b}" />
</p:graphicImage>
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
String a= externalContext.getRequestParameterMap().get("a");
String b= externalContext.getRequestParameterMap().get("b");
<p:dataTable  var="data" value="#{three.all}" ....
<p:graphicImage   alt="image"  value="#{three.getImage(data)}" cache="false"  >
                <f:param id="image_id" name="image_id" value="#{data.number}" />
</p:graphicImage></p:dataTable>
    for (int i=0; i<utlst.size(); i++ ){
             images.put(utlst.get(i).getNumber(), utlst.get(i).getImage());}
//utlst is the object retrieved from database. number is user-id.
public StreamedContent getImage(Util ut) throws IOException {
       //Util is the pojo

          String image_id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("image_id");
          System.out.println("image_id: " + image_id);

          if (image_id == null) {

                defaultImage=new DefaultStreamedContent(FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/Capture.PNG"), "image/png");

            return defaultImage; 
        }


         image= new DefaultStreamedContent(new ByteArrayInputStream(images.get(Integer.valueOf(image_id))), "image/png");

        return image;
    }