Help: Selecting Item on p:datatable

UI Components for JSF
Post Reply
Erick
Posts: 12
Joined: 26 Dec 2009, 22:30

07 Feb 2010, 21:51

I Managed to load my datatable using a method in my managed bean. The table populates correctly but i cant retrieve the selected row value. See code above:

Code: Select all


<h:form prependId="false">
                <H3><h:outputText value="Visualizar Periodo"/></H3>
                <h:panelGrid id="cadastraEvento" columns="2" border="0">
                    <h:outputText value="Data Inicial: "/>
                    <p:calendar value="#{FCAgenda.dataInicial}"/>
                    <h:outputText value="Data Final: "/>
                    <p:calendar value="#{FCAgenda.dataFinal}"/>

                <h:commandButton value="consultarAgenda" action="#{FCAgenda.buscaEvento}"/>

                </h:panelGrid>
                

                <p:dataTable var="listaAgenda" value="#{FCAgenda.listaAgenda}" paginator="true" rows="30"
                             selection="#{FCAgenda.itemAgendaSelecionado}" selectionMode="single">

                    <p:column>
                        <f:facet name="header">
                            <h:outputText value="Data" />
                        </f:facet>
                        <h:outputText value="#{listaAgenda.data}" />
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            <h:outputText value="Eventos" />
                        </f:facet>
                        <h:outputText value="#{listaAgenda.eventos}" />
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            <h:outputText value="Mensagens" />
                        </f:facet>
                        <h:outputText value="#{listaAgenda.mensagem}" />
                    </p:column>

                </p:dataTable>

                <p:commandButton value="Submit" update="display"/>

                <h:outputText id="display" value="#{itemAgendaSelecionado}" />
                               
            </h:form>

Managed Bean

Code: Select all


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FrontController;

import DA.DAEvento;
import VO.VOAgenda;
import VO.VOEvento;
import VO.VOUsuario;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.context.FacesContext;

/**
 *
 * @author Kinhu
 */
public class FCAgenda {

    private List<VOEvento> listaEvento;
    private List<VOAgenda> listaAgenda;
    private DAEvento daEvento;
    private VOAgenda voAgenda;
    private VOAgenda itemAgendaSelecionado;
    private Date dataInicial;
    private Date dataFinal;
    private VOUsuario voUsuario;

    public FCAgenda(){

        daEvento = new DAEvento();
        listaEvento = new ArrayList();
        dataInicial = new Date();
        dataFinal = new Date();
    
    }


    public void buscaEvento() {
        int numeroEventos=0;

        listaAgenda =  new ArrayList();

        voUsuario = (VOUsuario) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("voUsuario");
        listaEvento = daEvento.consultaEvento(dataInicial, dataFinal, voUsuario.getIdUsuario());



        for(Date dataI = new Date(getDataInicial().getTime());dataI.compareTo(getDataFinal())<0;dataI.setTime(dataI.getTime()+86400000)){

            numeroEventos=0;
            for(VOEvento voEvento : listaEvento){
                if(voEvento.getDataEvento().compareTo(dataI)==0)
                numeroEventos++;
            }

            voAgenda = new VOAgenda();
            voAgenda.setData(new SimpleDateFormat("dd/MM/yyyy").format(new Date(dataI.getTime())) );
            voAgenda.setEventos(numeroEventos);
            voAgenda.setMensagem(0);
            listaAgenda.add(voAgenda);

        }


        return;
    }

    /**
     * @return the listaEvento
     */
    public List<VOEvento> getListaEvento() {
        return listaEvento;
    }

    /**
     * @param listaEvento the listaEvento to set
     */
    public void setListaEvento(List<VOEvento> listaEvento) {
        this.listaEvento = listaEvento;
    }

    /**
     * @return the listaAgenda
     */
    public List<VOAgenda> getListaAgenda() {
        return listaAgenda;
    }

    /**
     * @param listaAgenda the listaAgenda to set
     */
    public void setListaAgenda(List<VOAgenda> listaAgenda) {
        this.listaAgenda = listaAgenda;
    }

    /**
     * @return the daEvento
     */
    public DAEvento getDaEvento() {
        return daEvento;
    }

    /**
     * @param daEvento the daEvento to set
     */
    public void setDaEvento(DAEvento daEvento) {
        this.daEvento = daEvento;
    }

    /**
     * @return the voAgenda
     */
    public VOAgenda getVoAgenda() {
        return voAgenda;
    }

    /**
     * @param voAgenda the voAgenda to set
     */
    public void setVoAgenda(VOAgenda voAgenda) {
        this.voAgenda = voAgenda;
    }

    /**
     * @return the dataInicial
     */
    public Date getDataInicial() {
        return dataInicial;
    }

    /**
     * @param dataInicial the dataInicial to set
     */
    public void setDataInicial(Date dataInicial) {
        this.dataInicial = dataInicial;
    }

    /**
     * @return the dataFinal
     */
    public Date getDataFinal() {
        return dataFinal;
    }

    /**
     * @param dataFinal the dataFinal to set
     */
    public void setDataFinal(Date dataFinal) {
        this.dataFinal = dataFinal;
    }

    /**
     * @return the voUsuario
     */
    public VOUsuario getVoUsuario() {
        return voUsuario;
    }

    /**
     * @param voUsuario the voUsuario to set
     */
    public void setVoUsuario(VOUsuario voUsuario) {
        this.voUsuario = voUsuario;
    }

    /**
     * @return the itemAgendaSelecionado
     */
    public VOAgenda getItemAgendaSelecionado() {
        return itemAgendaSelecionado;
    }

    /**
     * @param itemAgendaSelecionado the itemAgendaSelecionado to set
     */
    public void setItemAgendaSelecionado(VOAgenda itemAgendaSelecionado) {
        this.itemAgendaSelecionado = itemAgendaSelecionado;
    }

}



Erick
Posts: 12
Joined: 26 Dec 2009, 22:30

08 Feb 2010, 14:34

One More info about this problem:
I'm pasting here the exception that my application throws when i click in the SUBMIT button.
Can anyone help?

Thanks

Erick

08/02/2010 10:31:44 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalArgumentException
at javax.faces.model.ListDataModel.getRowData(ListDataModel.java:139)
at javax.faces.component.UIData.getRowData(UIData.java:325)
at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:67)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:764)
at javax.faces.component.UIData.processDecodes(UIData.java:937)
at org.primefaces.component.datatable.DataTable.processDecodes(DataTable.java:442)
at javax.faces.component.UIForm.processDecodes(UIForm.java:203)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1001)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:493)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

08 Feb 2010, 14:38

Hi,

What is the scope of your bean? Request, View, Session?

Erick
Posts: 12
Joined: 26 Dec 2009, 22:30

08 Feb 2010, 15:23

It's request, code above:

<managed-bean>
<description>Esse bean cadastra a agenda</description>
<managed-bean-name>FCAgenda</managed-bean-name>
<managed-bean-class>FrontController.FCAgenda</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

Do i have to set any other thing in my facex.xml or maybe add any jar in my classpath?? Now i'm using only the primefaces-1.0.0.jar on my classpath.

Thanks!

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

08 Feb 2010, 16:11

Can you try with session scope to see if it makes a difference. Because you might lose your backing data.

Erick
Posts: 12
Joined: 26 Dec 2009, 22:30

08 Feb 2010, 16:52

It almost worked.

I've changed to session scope but this works just for the 1st click on submit.
It retrieves the value correct but when i click againd the value does no change.

I've changed the code to:

Code: Select all

 <p:dataTable var="listaTabela" value="#{FCAgenda.listaAgenda}" paginator="true" rows="30"
                             selection="#{FCAgenda.teste}" selectionMode="single">
.
.
.columms
And the outputtext to:

Code: Select all

p:commandButton value="Submit" update="display"/>
<h:outputText id="display" value="#{FCAgenda.teste[0].data}" />
Any ideas?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 46 guests