我想让一个可切换的形式,其中每个机构从我的列表将在一个不同的可切换面板将包含照片+附加信息。可切换面板工作完美。我成功地采取机构从数据库和打印他们的名字,但我有问题,显示他们的图片在用户界面上。
下面是我的。xhtml页面中的表单:
<h:form>
<p:growl id="growl" showDetail="true" />
<c:forEach items="#{agencyBean.agencyList}" var="inputBoxes">
<p:fieldset legend="${inputBoxes.tUser.name}" toggleable="true"
toggleSpeed="500">
<p:ajax event="toggle" listener="#{fieldsetBean.handleToggle}" update="growl"/>
<h:panelGrid columns="2" cellpadding="10">
<p:graphicImage value="#{agencyBean.getAgencyPhoto(inputBoxes)}" />
Agency Name: <h:outputText value="${inputBoxes.tUser.name}"/>
Agency Phone:<h:outputText value="${inputBoxes.tUser.phone}"/>
</h:panelGrid>
</p:fieldset>
</c:forEach>
</h:form>
这是我的bean类:
@ManagedBean(name = "agencyBean")
@ViewScoped
public class AgencyBean implements Serializable{
private TAgency tAgency = new TAgency(); // + get and set method
private List<TAgency> agencyList; // + get and set method
@PostConstruct
public void init() {
EntityManager em = HibernateUtil.getEntityManager();
Query q = em.createQuery("select u from TAgency u");
agencyList = q.getResultList();
}
public StreamedContent getAgencyPhoto(TAgency agency) {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
} else {
if (agency.gettUser().getPhoto() == null) {
return new DefaultStreamedContent();
} else {
return new DefaultStreamedContent(new ByteArrayInputStream(
(agency.gettUser().getPhoto())));
}
}
}
}
它在Eclipse控制台给我的错误是:
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/MasterThesis] threw exception
java.io.IOException: java.lang.NullPointerException
at org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:122)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at bg.fmi.master.thesis.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:20)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
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)
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:242)
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71)
at org.apache.el.parser.AstValue.getValue(AstValue.java:147)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
at org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:85)
... 20 more
我稍微修改了一下代码,现在可以运行了:
<p:graphicImage value="#{agencyBean.agencyPhoto}">
<f:param name="agencyId" value="#{agency.tUser.id}" />
</p:graphicImage>
豆:
public StreamedContent getAgencyPhoto() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
} else {
String agencyId = context.getExternalContext()
.getRequestParameterMap().get("agencyId");
TAgency currentAgency = new TAgency();
for (TAgency currAgency : agencyList) {
if (currAgency.gettUserId() == Long.valueOf(agencyId))
currentAgency = currAgency;
}
byte[] image = (byte[]) currentAgency.gettUser().getPhoto();
if (image == null) {
return new DefaultStreamedContent();
} else {
return new DefaultStreamedContent(new ByteArrayInputStream(
image));
}
}
}