primefaces从数据库显示图像

时间:2012-11-05 04:36:16

标签: file-upload jsf-2 primefaces graphicimage

我正在尝试显示客户图像(存储在BLOB对象中的CustomerMaster.customerPhoto中)。我正在使用imageBuilder Bean(RequestScoped)来构建基于存储在客户Bean(ViewScoped)中可用的CustomerMaster中的内容的图像。我在ImageBuilder中添加了属性以访问客户Bean中的CustomerMaster对象。还添加了用于跟踪目的的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

更新

因为graphicImage标签被转换为<img> html标签。浏览器生成两个显示图像的请求。生成以下两个网址:

  1. /proj/views/user/CustomerRegistration.xhtml - ManagedProperty注释返回viewscoped customerBeans。

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

  3. 有什么建议吗?

0 个答案:

没有答案