gpt4 book ai didi

file-upload - primefaces 显示数据库中的图像

转载 作者:行者123 更新时间:2023-12-02 02:10:25 27 4
gpt4 key购买 nike

<分区>

我正在尝试显示客户图像(存储在 BLOB 对象中的 CustomerMaster.customerPhoto 中)。我正在使用 imageBuilder Bean (RequestScoped) 根据存储在 CustomerMaster 中的内容构建图像在 Customer Bean(ViewScoped) 中可用。我在 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)中使用 Session 范围,则一切都是工作文件。甚至图像显示在网页上。根据我的理解,从 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();
}
}
}
}
}

客户.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>标签。浏览器生成两个显示图像的请求。生成以下两个 url:

  1. /proj/views/user/CustomerRegistration.xhtml - ManagedProperty 注释返回 View 范围的 customerBeans。

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

有什么建议吗?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com