PrimeFaces数据表不起作用 [英] PrimeFaces DataTables not working

查看:77
本文介绍了PrimeFaces数据表不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PrimeFaces的新手,他在PrimeFaces数据表中尝试了一个示例

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>

这是xhtml页面,用于通过数据表获取Bean值,但是JSF显示的是一个空页面.任何帮助将不胜感激.

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.

推荐答案

尝试一下并让我们知道(使用 CDI ,删除无用的构造函数并正确初始化addeta)

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>

现在,请确保已将Primefaces库放置在正确的位置,并且该应用已正确部署在正在运行的服务器上.

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.

编辑:之所以没有数据,是因为Datatable的构造函数不完整.

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

替换

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

这篇关于PrimeFaces数据表不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆