我使用相同的代码使用p:graphicImage显示图像,但我无法找到它无效的原因。
我之前使用的是这个代码,因为在我的应用程序中,并不是每个数据库条目都有一个映像,所以我这样做是为了避免空blob。
现在我重写它与Display dynamic image from database with p:graphicImage and StreamedContent中的答案完全相同,但它仍然无效:(
我使用完全相同的代码在完全相同的p中显示图像:在另一个.xhtml中重复并且它完美地工作。
在调试会话期间,我看到它调用另一个在.xhtml中没有任何引用的ManagedBean。
@ManagedBean
@ViewScoped
public class PatenteService implements Serializable {
private StreamedContent fotoPatente;
public StreamedContent getFotoPatente() throws IOException {
String inputId = "";
Patente p;
byte[] bytes;
FacesContext context = FacesContext.getCurrentInstance();
byte[] defaultBytes; // ... Retrieve deafult image to show if db resgistry has it not
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent(new ByteArrayInputStream(defaultBytes));
} else {
inputId = context.getExternalContext().getRequestParameterMap().get("patenteId");
if (!inputId.equals("")) {
p = (Patente) EManager.getInstance().createNamedQuery("Patente.findById").setParameter("id", Integer.parseInt(inputId)).getSingleResult();
bytes = p.getFoto();
} else {
bytes = null;
}
if (bytes != null) {
return new DefaultStreamedContent(new ByteArrayInputStream(bytes));
} else {
return new DefaultStreamedContent(new ByteArrayInputStream(defaultBytes));
}
}
}
public void setFotoPatente(StreamedContent fotoPatente) {
this.fotoPatente = fotoPatente;
}
}
错误:
01-Sep-2017 18:13:19.716 GRAVE [http-nio-8080-exec-7] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Faces Servlet] in context with path [/prorec] threw exception
java.io.IOException: java.lang.NullPointerException
at org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:103)
at org.primefaces.application.resource.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:95)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
...
Caused by: java.lang.NullPointerException
at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:566)
at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477)
01-Sep-2017 18:13:19.868 GRAVE [http-nio-8080-exec-4] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Faces Servlet] in context with path [/prorec] threw exception
java.io.IOException: java.lang.NullPointerException
at org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:103)
at org.primefaces.application.resource.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:95)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
...
Caused by: java.lang.NullPointerException
at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:566)
at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477)
at com.sun.faces.mgbean.BeanManager.getBeanFromScope(BeanManager.java:240)
01-Sep-2017 18:13:19.715 GRAVE [http-nio-8080-exec-7] org.primefaces.application.resource.StreamedContentHandler.handle Error in streaming dynamic resource.
java.lang.NullPointerException
at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:566)
at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477)
at com.sun.faces.mgbean.BeanManager.getBeanFromScope(BeanManager.java:240)
...
01-Sep-2017 18:13:19.867 GRAVE [http-nio-8080-exec-4] org.primefaces.application.resource.StreamedContentHandler.handle Error in streaming dynamic resource.
java.lang.NullPointerException
at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:566)
at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477)
at com.sun.faces.mgbean.BeanManager.getBeanFromScope(BeanManager.java:240)