我正在尝试打印一个简单的dataTable。我已经创建了两个类Product和ProductController,我将List传递给但是每当我运行代码时,它只打印dataTable列标题值,但不打印应该从List中获取的数据。
我正在使用Wildfly 10.0.1服务器
index.xhtml代码
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<body>
<h:form>
<h:dataTable var="p" value="#{ProductController.findAll()}" border = "1" cellpadding="2">
<h:column>
<f:facet name="header">Id</f:facet>
<h:outputText value="#{p.id}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{p.name}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
<h:outputText value="#{p.price}"></h:outputText>
</h:column>
</h:dataTable>
</h:form>
</body>
</html>
产品类
package entities;
public class Product {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
System.out.println("I am here");
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Product(String id, String name) {
super();
this.id = id;
this.name = name;
//this.price = price;
}
public Product() {
super();
}
}
ProductController类
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import entities.Product;
@ManagedBean
@SessionScoped
public class ProductController {
private Product product = new Product();
public ProductController() {
super();
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public List<Product> findAll()
{
List<Product> listProduct = new ArrayList<Product>();
try {
listProduct.add (new Product("p1", "product1"));
listProduct.add (new Product("p2", "product2"));
listProduct.add (new Product("p3", "product3"));
}
catch(NullPointerException e)
{
System.out.println("its Null");
}
return listProduct;
}
}
ps:这是youtube视频代码,但它没有在我的机器上运行,