Jsf 如何从managedbean绑定primefaces数据表

Jsf 如何从managedbean绑定primefaces数据表,jsf,jsf-2,primefaces,datatable,Jsf,Jsf 2,Primefaces,Datatable,如何在managedbean中绑定primefaces数据表?如何放置数据,以及如何放置列 我的bean类: public class BeanTest implements Serializable{ private String name; private String email; private int age; //getters and setters } 我的托管bean: public class TestTable implements Serializable{ priva

如何在managedbean中绑定primefaces数据表?如何放置数据,以及如何放置列

我的bean类:

public class BeanTest implements Serializable{
private String name;
private String email;
private int age;
//getters and setters
}
我的托管bean:

public class TestTable implements Serializable{
private DataTable tabela;
private List<BeanTest> lista;

@PostConstruct
public void init() {
int age= 18;
this.lista = new ArrayList<>();
this.lista.add(new BeanTest("name1", "email1", age));
this.lista.add(new BeanTest("name2", "email2", age++));
this.lista.add(new BeanTest("name3", "email3", age++));

this.tabela = new DataTable();
Column column1 = new Column();
column1.setHeaderText("Nome");

Column column2 = new Column();
column2.setHeaderText("Email");

Column column3 = new Column();
column3.setHeaderText("Idade");

this.getTabela().getChildren().add(column1);
this.getTabela().getChildren().add(column2);
this.getTabela().getChildren().add(column3);

this.getTabela().setValue(this.lista);
}
}
公共类TestTable实现可序列化{
私有数据表tabela;
私人名单清单a;
@施工后
公共void init(){
年龄=18岁;
this.lista=新的ArrayList();
add(新BeanTest(“name1”,“email1”,age));
add(新的BeanTest(“name2”,“email2”,age++);
add(新的BeanTest(“name3”,“email3”,age++);
this.tabela=新数据表();
Column column1=新列();
第1列:设置标题文本(“Nome”);
Column column2=新列();
第2列:设置标题文本(“电子邮件”);
Column column3=新列();
第3列:设置标题文本(“Idade”);
this.getTabela().getChildren().add(第1列);
this.getTabela().getChildren().add(第2列);
this.getTabela().getChildren().add(第3列);
this.getTabela().setValue(this.lista);
}
}
JSF页面:

<p:dataTable id="datalist" binding="#{testeTabela.tabela}">
</p:dataTable>

这将显示包含三列(正确、数字和标题)和三行(正确数字)的表,但“我的行”中没有数据。仅带边框单元格的空表


发生了什么事?如何绑定列和数据?

一般来说,JSF组件有3个部分:标记、组件类和渲染器

标签负责组件配置。它将实例化组件并设置适当的属性、侦听器和方面。配置后,组件将放在组件树上

使用您的示例,页面代码将类似于以下内容:

<p:dataTable id="dataTable" var="item" value="#{bean.list}">
    <p:column headerText="Name">#{item.name}</p:column>
    <p:column headerText="Email">#{item.email}</p:column>
    <p:column headerText="Age">#{item.age}</p:column>
</p:dataTable>
其次,在列中添加一个子UIOutput,并在itsvalue属性中添加一个表达式。对于“名称”列,它类似于以下内容:

FacesContext context = FacesContext.getCurrentInstance();
//Creates the output and sets the value to an expression language
UIOutput output1 = new UIOutput();
output1.setValueExpression("value",context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(),"#{item.name}", String.class));
//Add the output to the column
column1.getChildren().add(output1);
对于其他列,除了第三个UIOutput的值的类型外,想法是相同的:

...createValueExpression(context.getELContext(),"#{item.age}", Integer.class));
正如您可能看到的,这可能很难维护


使用标记更干净、更容易阅读。

一般来说,JSF组件有三个部分:标记、组件类和渲染器

标签负责组件配置。它将实例化组件并设置适当的属性、侦听器和方面。配置后,组件将放在组件树上

使用您的示例,页面代码将类似于以下内容:

<p:dataTable id="dataTable" var="item" value="#{bean.list}">
    <p:column headerText="Name">#{item.name}</p:column>
    <p:column headerText="Email">#{item.email}</p:column>
    <p:column headerText="Age">#{item.age}</p:column>
</p:dataTable>
其次,在列中添加一个子UIOutput,并在itsvalue属性中添加一个表达式。对于“名称”列,它类似于以下内容:

FacesContext context = FacesContext.getCurrentInstance();
//Creates the output and sets the value to an expression language
UIOutput output1 = new UIOutput();
output1.setValueExpression("value",context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(),"#{item.name}", String.class));
//Add the output to the column
column1.getChildren().add(output1);
对于其他列,除了第三个UIOutput的值的类型外,想法是相同的:

...createValueExpression(context.getELContext(),"#{item.age}", Integer.class));
正如您可能看到的,这可能很难维护


使用标记更干净、更容易阅读。

避免使用
绑定
和繁琐的Java代码方式,直到/除非在某些(非常)特殊情况下绝对必要,而在这种情况下,这些情况似乎是绝对多余的。查看网页上的示例。(不要忘记单击该页面上的相应链接)。避免使用
绑定
和繁琐的Java代码方式,直到/除非在某些(非常)特殊情况下绝对必要,而在这种情况下,这些情况似乎是绝对多余的。查看网页上的示例。(不要忘记单击该页面上的相应链接)。