javaworld
javaworld

Reputation: 435

PrimeFaces DataTables not working

I'm new to PrimeFaces tried out an example in PrimeFaces datatable

public class Datatable {
    private String fname;
    private String lname;
    private int age;
    public Datatable(String fname, String lname, int age) {
        // TODO Auto-generated constructor stub
    }
    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public String getLname() {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

Here in the class I have declared what are the fields in the data table

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="solodat")
@RequestScoped
public class Solodata implements Serializable{

    private static final long serialVersionUID = 1L;

    public Solodata() {}

    private List<Datatable>addeta;

    public List<Datatable> getAddeta() {
        return addeta;
    }

    public void setAddeta(List<Datatable> addeta) {
        this.addeta = addeta;
    }

    @PostConstruct
    public void init() {
        List<Datatable> addeta=new  ArrayList<Datatable>();
        addeta.add( new Datatable("man","eater",14));
        addeta.add( new Datatable("solo","world",28));
        addeta.add( new Datatable("antan","evanious",20));
        addeta.add( new Datatable("hi","daa",29));
        addeta.add( new Datatable("thallu","vandi",30));
        addeta.add( new Datatable("prime","faces",1000));
        addeta.add( new Datatable("crime","shit",1412));
        addeta.add( new Datatable("shit","head",18));
    }
}

Here in list I have get that values:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://www.java.com/jsf/html"
 xmlns:f="http://www.java.com/jsf/core"
 xmlns:p="http://www.primefaces.org/ui">
  <h:head>
<title>DATA TABLES DEMO</title>
</h:head>
<h:body>
<h:form>
<h1>output values</h1>
<p:dataTable var="sol" value="#{solodat.addeta}"  >

<p:column headerText="LASTNAME">
<h:outputText value="#{sol.lname}"/>

</p:column>
<p:column  headerText="age">

<h:outputText value="#{sol.age}"/>

</p:column>
<p:column headerText="first">
<h:outputText value="#{sol.fname}"/>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>

This is the xhtml page to get the bean values by data table but JSF is showing an empty page. Any help would be appreciated.

Upvotes: 1

Views: 6090

Answers (3)

user2880020
user2880020

Reputation:

Try this and let us know (using CDI, removing the useless constructor, and initializing addeta properly)

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

@Named("solodat")
@RequestScoped
public class Solodata {

    private List<Datatable> addeta;

    public List<Datatable> getAddeta() {
        return addeta;
    }

    public void setAddeta(List<Datatable> addeta) {
        this.addeta = addeta;
    }

    @PostConstruct
    public void init() {
        addeta=new  ArrayList<Datatable>();
        addeta.add( new Datatable("man","eater",14));
        addeta.add( new Datatable("solo","world",28));
        addeta.add( new Datatable("antan","evanious",20));
        addeta.add( new Datatable("hi","daa",29));
        addeta.add( new Datatable("thallu","vandi",30));
        addeta.add( new Datatable("prime","faces",1000));
        addeta.add( new Datatable("crime","shit",1412));
        addeta.add( new Datatable("shit","head",18));
    }
}

And replace the facelet with this one (I changed the first lines)

<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
  <h:head>
<title>DATA TABLES DEMO</title>
</h:head>
<h:body>
<h:form>
<h1>output values</h1>

<p:dataTable var="sol" value="#{solodat.addeta}"  >

<p:column headerText="LASTNAME">
<h:outputText value="#{sol.lname}"/>
</p:column>

<p:column  headerText="age">
<h:outputText value="#{sol.age}"/>
</p:column>

<p:column headerText="first">
<h:outputText value="#{sol.fname}"/>
</p:column>

</p:dataTable>
</h:form>
</h:body>
</html>

Now make sure you have placed Primefaces library at the right place, and that the app is being deployed on a running server without errors.

Edit: The reason you don't have data , is due to Datatable's constructor, which is incomplete.

Replace

public Datatable(String fname, String lname, int age) {
        // TODO Auto-generated constructor stub
    }

by

public Datatable(String fname, String lname, int age) {
       this.fname = fname;
       this.lname = lname;
       this.age = age;
    }

Upvotes: 2

wittakarn
wittakarn

Reputation: 3164

You should use <!DOCTYPE html> instead of

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

and also change your are initializing as noone answer.

See also : Wrong doctype when one is specified in composite view

Upvotes: 2

noone
noone

Reputation: 19776

Small mistake: You are initializing a new local variable in init().

List<Datatable> addeta = new ArrayList<Datatable>();

Change it to:

this.addeta = new ArrayList<Datatable>();

Upvotes: 2

Related Questions