PrimeFaces从数据库显示图像



我正在尝试显示客户映像(存储在blob对象中的CustomerMaster.customerPhoto中)。我正在使用ImageBuilder Bean(requestScoped)根据CustomerMaster中存储的内容来构建图像,该内容可在客户bean(viewScoped)中使用。我在ImageBuilder中添加了属性,以访问CustomerBaster对象中的CustomerBaster对象。还添加了用于跟踪目的的Sysout语句。

这是

的输出
09:34:22,817 INFO  [stdout] (http--127.0.0.1-8080-2) getImage - 1
09:34:22,817 INFO  [stdout] (http--127.0.0.1-8080-2) getImage - 3
09:34:22,817 INFO  [stdout] (http--127.0.0.1-8080-2) getImage - 4
09:34:22,817 INFO  [stdout] (http--127.0.0.1-8080-2) getImage - 5PhotoMaster [photoId=1,  contentType=image/gif]    2064
09:34:22,817 INFO  [stdout] (http--127.0.0.1-8080-2) getImage - 6
09:34:22,827 WARNING [javax.enterprise.resource.webcontainer.jsf.context] (http--127.0.0.1-8080-2) JSF1091: No mime type could be found for file dynamiccontent.  To resolve this, add a mime-type mapping to the applications web.xml.
09:34:23,057 SEVERE [org.primefaces.application.PrimeResourceHandler] (http--127.0.0.1-8080-4) Error in streaming dynamic resource. Unable to set property customerBean for managed bean imageBuilderBean

基于Sysout语句,我可以看到ImageBuilderBean能够访问Customermaster对象并能够创建DefaultStreamedContent。

,但后来我将遵循严重消息,并且图像不会在网页上放置

09:34:23,057 SEVERE [org.primefaces.application.PrimeResourceHandler] (http--127.0.0.1-8080-4) Error in streaming dynamic resource. Unable to set property customerBean for managed bean imageBuilderBean

如果我在CustomerBean中使用会话范围(而不是ViewScoped),则所有内容都在工作文件。甚至图像都显示在网页上。根据我的理解,从RequestScoped Bean调用ViewScoped Bean时不应该有任何问题。

我不确定怎么了。请帮忙。请参阅代码以获取参考。

imageBuilder.java

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
    @ManagedBean (name="imageBuilderBean")
    @RequestScoped
    public class ImageBuilderBean implements Serializable {
        private static final long serialVersionUID = -480089903900643650L;
        @ManagedProperty(value="#{customerBean}")    
        private CustomerBean customerBean;
        private StreamedContent image;
        public ImageBuilderBean() {
            super();
        }
        @PostConstruct
        void ResetBean(){
            System.out.println("getImage - 1");
            FacesContext context = FacesContext.getCurrentInstance();
            if (context.getRenderResponse()) {
                System.out.println("getImage - 2");
                // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
                image = new DefaultStreamedContent();
            }
            else {
                System.out.println("getImage - 3");
                CustomerMaster custMaster = customerBean.getCustMaster();
                if (custMaster != null){
                    System.out.println("getImage - 4");
                    PhotoMaster photo = custMaster.getCustomerPhoto();
                    if (photo != null) {
                        try {
                            System.out.println("getImage - 5" + photo + "    " + photo.getContent().length());
                            InputStream inputStream = photo.getContent().getBinaryStream();
                            image = new DefaultStreamedContent(inputStream, photo.getContentType());
                            System.out.println("getImage - 6");
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            System.out.println("exception Shirish");
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

customer.xhtml

<p:column>
    <p:graphicImage id="custImageId" value="#{imageBuilderBean.image}" cache="false" />
</p:column> 

customerbean.java

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
@ManagedBean (name="customerBean")
@ViewScoped
public class CustomerBean implements Serializable {
private static final long serialVersionUID = -3727342589028832013L;
// Setters and Getters

更新:

将图形图标签转换为<img> HTML标签。浏览器生成了两个请求以显示图像。以下两个URL生成:

  1. /proj/views/user/CustomerRegistration.xhtml-托管property注释返回viewscoped customerbeans。

  2. /proj/javax.faces.resource/dynamiccontent.xhtml-无法返回CustomerBean。因此,我们看到"无法为托管bean imageBuilderBean设置属性customerbean"错误消息

有什么建议?

该问题放在viewscoped and sessionscoped bean的逻辑中。

在RequestScoped Bean中,所有请求均由New Bean实例
处理在sessionscoped bean中,所有携带同一会话的请求均由同一实例进行处理。

ViewScoped Bean在这两个之间是"。它创建了"伪session",就像在bean中一样工作,但它以其他方式获取。它的处理方式与浏览器发送会话信息(例如以cookie或其他方式)中的会话范围的豆类的处理方式相同,但它保留在" webpage"本身(在javascript中),因此对于每个ajax请求,请通过网页发送它添加是它自己的" vievsessionkey"(每个视图的唯一)。

显示页面时,浏览器发送网页请求,然后浏览器(非网页)发送另一个图片的请求。在此情况下

如果要在ViewScoped Bean上进行此类操作,则需要在每个Bean的每个新实例之间实现携带数据的方法。您可以尝试制作某种携带图片数据或某些静态字段的Singleton Manager。但这是解决此问题的"丑陋"方法,更合乎逻辑的方法是使您的bean sessecon范围范围...或至少将某些功能与新的会话范围示波了bean。

ps:我知道我挖出了一些恐龙问题,但是我遇到了同样的问题,在阅读您的问题时,我意识到了javaScripts/javaScripts/browser为每种bean type

所做的事情

最新更新