如何使用PrimeFaces在UI上可视化图片

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用PrimeFaces在UI上可视化图片相关的知识,希望对你有一定的参考价值。

我想创建一个可切换的表单,我的列表中的每个代理都将位于不同的可切换面板中,该面板将包含照片+其他信息。可切换面板非常完美。我成功地从数据库中取出代理并打印他们的名字,但我在用户界面上显示他们的图片时遇到问题。

这是我的.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));
            }
        }
    }

以上是关于如何使用PrimeFaces在UI上可视化图片的主要内容,如果未能解决你的问题,请参考以下文章

Primefaces 中全局更改的默认样式

如何使用自定义样式覆盖默认的 PrimeFaces CSS?

primefaces中的多个图像上传

PrimeFaces 4.0 PF未定义

如何在 Primefaces+JSF2 中显示用户点击的 url

JSF Primefaces TabView 问题