0
votes

Edit2 - I've added the faces-config.xml at the end of the post.

I'm having problems with Primefaces datatable row selection. I want to be able to select a row and move the data into an object that I can then manipulate. I'm using a model based on the primefaces showcase example, but it doesn't work. Frankly, I'm running out of ideas as to what is wrong. Below is my xhtml and managedbean.

<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui" >
<h:head>

</h:head>
<h:body>
<center>
<h:form id="form">

<p:dataTable id="personTable" var="client" value="#{tableBean.persons}" rowKey="#{client.name}"
             selection="#{tableBean.person}" selectionMode="single">

    <f:facet name="header">
        Click "View" button after selecting a row to see details
    </f:facet>

    <p:column headerText="Name">
        #{client.name}
    </p:column>

    <p:column headerText="Address">
        #{client.address}
    </p:column>

    <p:column headerText="Phone" >
        #{client.phone}
    </p:column>
</p:dataTable>

<h:panelGrid id="display" columns="2" cellpadding="4">


        <h:outputText value="Name:" />
        <h:outputText value="#{tableBean.person.name}" />

        <h:outputText value="Address:" />
        <h:outputText value="#{tableBean.person.address}" />

        <h:outputText value="Phone:" />
        <h:outputText value="#{tableBean.person.phone}" />

</h:panelGrid>

</h:form>

</center>
</h:body>
</html>

Managed Bean here:

package com.dave.test;

import java.util.ArrayList;
import java.util.List;

public class TableBean {

private List<Person> persons = null;
private Person person;

public TableBean() {
    persons = new ArrayList<Person>();
    persons.add(new Person("Jimmy", "18 Maple", "337-278-1019"));
    persons.add(new Person("Sally", "47 Oak", "787-509-3819"));
    persons.add(new Person("Roger", "754 Fifth Ave.", "926-420-8219"));
    persons.add(new Person("Mimi", "891 2nd St.", "713-371-8632"));

}

public List<Person> getPersons() {
    return persons;
}

public void setPersons(List<Person> persons) {
    this.persons = persons;
}

public Person getPerson() {
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}


}

Thanks, Dave

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-    
 facesconfig_2_0.xsd">
 <managed-bean>
   <managed-bean-name>tableBean</managed-bean-name>
   <managed-bean-class>com.dave.test.TableBean</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
</faces-config>
2
Are your managed beans being declared in faces-config.xml or did you forget the annotations? We need to see why the managed bean is not retaining data after a server request. Could you please include your faces-config.xml?maple_shaft
Thanks for responding, I just added it at the end of my post.Dave
I'm not seeing your faces-config.xml file @Dave.Catfish
Off-topic, but you know the <center></center> tag is deprecated right?Catfish
Okay, it's in there now (got lost the first time somehow). As for center, yes, but this is just a test program for Primefaces. I'll fancy it up after I get this issue set in my mind.Dave

2 Answers

0
votes

I'm assuming that when you click the row, there is no data. That is because you are using a request scoped bean. This means that when you load the page, the bean is populated. After the page is loaded, the bean is gone.

I would suggest changing your scope to ViewScope to see if that helps at all.

Also, if you're using jsf 2.0, you can use annotations instead of the faces-config.xml file. Your backer would look like this:

package com.dave.test;

import java.util.ArrayList;
import java.util.List;

public class TableBean {

private List<Person> persons = null;
private Person person;

@ManagedBean
@ViewScoped
public TableBean() {
    persons = new ArrayList<Person>();
    persons.add(new Person("Jimmy", "18 Maple", "337-278-1019"));
    persons.add(new Person("Sally", "47 Oak", "787-509-3819"));
    persons.add(new Person("Roger", "754 Fifth Ave.", "926-420-8219"));
    persons.add(new Person("Mimi", "891 2nd St.", "713-371-8632"));

}

public List<Person> getPersons() {
    return persons;
}

public void setPersons(List<Person> persons) {
    this.persons = persons;
}

public Person getPerson() {
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}


}

Now you can remove your managed bean stuff from faces-config.xml.

EDIT I just realized you don't have an ajax event to handle the row selection. If you're looking at the primefaces instant row selection, you need to notice that they are using <p:ajax event="rowSelect" ..../> along with a method in the backing bean to handle this.

0
votes

lo único que debes hacer es al bean TableBean.java quitarle(borrar) el @SessionScoped y a la clase Car.java quitarle @ManagedBean(name = "car") , @SessionScoped , implements Serializable. esta clase es una simple clase no tiene porque ser un managebean unicamente son datos.

Translated:

All you have to do is to take TableBean.java bean (delete) the @SessionScoped and take Car.java class @ManagedBean (name = "car"), @SessionScoped, implements Serializable. This class is a simple class need not be a managebean only are data.