primefaces不使用lazyModel显示dataTable值

时间:2015-03-03 13:12:12

标签: jsf primefaces

我从primefaces开始,我尝试在p:dataTable中使用LazyModel。

我已经实现了LazyModel,bean和jsf。 bean和模型的调用正确发生,我的bean返回一个包含元素的列表,但是我的jsf没有显示任何内容。

拜托,有人知道会发生什么事吗?

贝娄是我的代码:

JSF:

<ui:composition template="./newTemplate.xhtml">
        <ui:define name="content">
            content
            <h:form>
                <p:panel id="formFiltro">
                    <p:messages id="messages"/>
                    <h:panelGrid>
                        <h:outputLabel for="fieldConta" value="Número da Conta:"/>
                        <p:inputText id="fieldConta" value="#{log.nrConta}" label="Número da Conta">
                            <f:convertNumber integerOnly="true" type="number"/>
                        </p:inputText>
                        <!--<p:message for="fieldConta" />-->

                        <h:outputLabel for="fieldAgencia" value="Código da Agência:"/>
                        <p:inputText id="fieldAgencia" value="#{log.nrAgencia}" label="Código da Agência">
                            <f:convertNumber integerOnly="true" type="number"/>
                        </p:inputText>
                        <!--<p:message for="fieldAgencia" />-->

                        <center>
                            <p:commandButton ajax="false" value="Pesquisar" action="#{log.search}" />
                        </center>
                    </h:panelGrid>
                </p:panel>

                <p:dataTable var="l" value="#{log.lazyLogModel}" paginator="true" rows="5"
                     paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                     rowsPerPageTemplate="5,10,15" id="logTable" lazy="true">

            <p:column headerText="ID">
                <h:outputText value="#{l.logId}" />
            </p:column>
            <p:column headerText="Agência">
                <h:outputText value="#{l.logAgencia}" />
            </p:column>
            <p:column headerText="Conta">
                <h:outputText value="#{l.logConta}" />
            </p:column>
            <p:column headerText="SO">
                <h:outputText value="#{l.logSo}" />
            </p:column>
            <p:column headerText="Plugin">
                <h:outputText value="#{l.logVersaoPlugin}" />
            </p:column>                
            <p:column headerText="Tam F10">
                <h:outputText value="#{l.logTamF10}" />
            </p:column>                                
        </p:dataTable>
            </h:form>
        </ui:define>
    </ui:composition>

我的豆子:

 @ManagedBean(name="log")
    @RequestScoped

    public class consultaJsf implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * Creates a new instance of consultaJsf
     */
    private List<TbLogLog> listaLog;

    private LazyLogModel lazyLogModel;

    private int nrConta;

    private int nrAgencia;

    public consultaJsf() {
        try{
            //this.listaLog = new ConsultarDados().getListLogAll();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @PostConstruct
    public void init()
    {
        this.lazyLogModel = new LazyLogModel();
    }

    public List<TbLogLog> getListaLog()
    {
        return listaLog;
    }

    public int getNrConta()
    {
       return nrConta;
    }

    public void setNrConta(int nrConta)
    {
       this.nrConta = nrConta;
    }

    public int getNrAgencia()
    {
        return nrAgencia;
    }

    public void setNrAgencia(int nrAgencia)
    {
        this.nrAgencia = nrAgencia;
    }

    public LazyLogModel getLazyLogModel()
    {
        return this.lazyLogModel;
    }

    public String search() throws Exception
    {
        if(nrConta != 0)
            this.listaLog = new ConsultarDados().getListLogByConta(nrConta);
        else if(nrAgencia != 0)
            this.listaLog = new ConsultarDados().getListLogByAgencia(nrAgencia);

        return null;
    }
}

My LazyModel:

public class LazyLogModel extends LazyDataModel<TbLogLog> {

    private String nrConta;
    private String cdAgencia;


    @Override
    public List<TbLogLog> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        List<TbLogLog> listaLog = null;
        try{
            listaLog = new ConsultarDados().getListLogAll(first, pageSize);
        }catch(Exception e){
            return null;
        }
        return listaLog;
    }

    /**
     * @return the nrConta
     */
    public String getNrConta() {
        return nrConta;
    }

    /**
     * @param nrConta the nrConta to set
     */
    public void setNrConta(String nrConta) {
        this.nrConta = nrConta;
    }

    /**
     * @return the cdAgencia
     */
    public String getCdAgencia() {
        return cdAgencia;
    }

    /**
     * @param cdAgencia the cdAgencia to set
     */
    public void setCdAgencia(String cdAgencia) {
        this.cdAgencia = cdAgencia;
    }



}

咨询LazyModel调用的方法:

  private List<TbLogLog> getListLog(String hql, int firstResult, int sizePage) throws Exception {
                List resultList = null;
        try {
            Session session = HubernateUtil.getSessionFactory().openSession();
            if ((hql == null) || (hql.trim().length() == 0)) {
                hql = QUERY_PESQUISAR_TODOS;
            }

            Query q = session.createQuery(hql);
            if(firstResult > 0 )
                q.setFirstResult(firstResult);
            if(sizePage > 0)
                q.setMaxResults(sizePage);

            resultList = q.list();
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        return resultList;
    }

一切正常,但我的jsf没有显示任何结果。

提前致谢。

1 个答案:

答案 0 :(得分:0)

删除 consultaJsf类中的 init(),并按如下所示更新 getLazyLogModel()

@ManagedBean(name="log")
@RequestScoped

public class consultaJsf implements Serializable {

private static final long serialVersionUID = 1L;
/**
 * Creates a new instance of consultaJsf
 */
private List<TbLogLog> listaLog;

private LazyLogModel<TbLogLog> lazyLogModel;

private int nrConta;

private int nrAgencia;

public consultaJsf() {
    try{
        //this.listaLog = new ConsultarDados().getListLogAll();
    }catch(Exception e){
        e.printStackTrace();
    }
}

public List<TbLogLog> getListaLog()
{
    return listaLog;
}

public int getNrConta()
{
   return nrConta;
}

public void setNrConta(int nrConta)
{
   this.nrConta = nrConta;
}

public int getNrAgencia()
{
    return nrAgencia;
}

public void setNrAgencia(int nrAgencia)
{
    this.nrAgencia = nrAgencia;
}

public LazyLogModel<TbLogLog> getLazyLogModel()
{        
   if (lazyLogModel== null) {
        lazyLogModel= new LazyDataModel<TbLogLog>() {
            @Override
            public List<TbLogLog> load(int first, int pageSize, String sortField,
                    SortOrder sortOrder, Map<String, String> filters) {
              List<TbLogLog> listaLog = null;
              try{
                  listaLog = new ConsultarDados().getListLogAll(first, pageSize);
             }catch(Exception e){
                  return null;
           }
              return listaLog;
            }
        };
    }
    return listaLog; 
}

public String search() throws Exception
{
    if(nrConta != 0)
        this.listaLog = new ConsultarDados().getListLogByConta(nrConta);
    else if(nrAgencia != 0)
        this.listaLog = new ConsultarDados().getListLogByAgencia(nrAgencia);

    return null;
}

}