2014-12-06 107 views
1

數據表我是新來PrimeFaces嘗試了在PrimeFaces數據表爲例PrimeFaces不工作

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; 
    } 
} 

在這裏,我已經聲明的類是什麼在數據表中

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)); 
    } 
} 

這裏領域在名單上有獲取值:

<?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> 

這是XHTML頁面獲得通過數據bean的值表,但JSF顯示空白頁面。任何幫助,將不勝感激。

+0

什麼服務器,你運行的?是否有任何primeface標籤正在工作? – 2014-12-06 13:14:02

回答

2

試試這個,讓我們知道(使用CDI,在刪除無用的構造函數,並初始化addeta正確)

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)); 
    } 
} 

,代之以這一個facelet裏(我改變了第一行)

<!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> 

現在確保您已經將Primefaces庫放置在正確的地方,並且該應用程序正在部署在正在運行的服務器上而沒有錯誤。

編輯:你沒有數據的原因,是由於Datatable的構造函數,它是不完整的。

更換

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

通過

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

是的工作,但沒有數據(沒有找到記錄) – javaworld 2014-12-06 17:13:55

+0

哦,我以前沒見過的愚蠢的錯誤。檢查數據表的構造函數..你沒有看到任何錯誤?讓我編輯。 – 2014-12-06 17:46:15

+0

它工作的原因,它沒有工作它becauese我打電話給他們在蠕蟲順序後1)fname 2)lname 3)年齡的存儲順序,它存儲在它的工作陣列列表 – javaworld 2014-12-06 17:55:27

2

小錯誤:您正在初始化一個新的本地變量在init()

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

將其更改爲:

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

仍然存在相同的問題 – javaworld 2014-12-06 11:52:05