I am trying to display Customer Image (Stored in CustomerMaster.customerPhoto
in BLOB object). I am using imageBuilder Bean (RequestScoped) to construct image based on contents stored in CustomerMaster
which is available in Customer Bean(ViewScoped). I have added property in ImageBuilder to access CustomerMaster Object in customer Bean. Also added sysout statements for tracing purpose.
Here is output of
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
Based on sysout statements, I can see ImageBuilderBean is able to access CustomerMaster Object and able to create DefaultStreamedContent.
But Later on I am getting following severe message and image is not displyed on web page:
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
If I use Session scope in CustomerBean (instead of ViewScoped) everything is working file. Even Image is displayed on web page. As per my understanding there should not be any issues in calling Viewscoped bean from requestScoped Bean.
I am not sure what is wrong. Please help. Please see code for reference.
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
Update:
As graphicImage tag is converted to <img>
html tag. Browser generates two requests to display image. Following two urls are generated:
/proj/views/user/CustomerRegistration.xhtml
- ManagedProperty annotations returns viewscoped customerBeans./proj/javax.faces.resource/dynamiccontent.xhtml
- Failed to return customerBean. Hence we are seeing "Unable to set property customerBean for managed bean imageBuilderBean" error message
Any suggestions?