0

I would fill datatable, but jsf does not show items in table, why? Thank you in advance who will be able to help me. I can't understand what's wrong with my code. My Java class in model

public class Product {  
    private int id;    
    private String name;    
    private float price;    

    public Product(int id, String name, float price) {    
        this.id = id;    
        this.name = name;    
        this.price = price;    
    }  

    public int getId() {  
        return id;  
    } 

    public String getName() {  
        return name;  
    } 

    public float getPrice() {  
        return price;  
    }  
}

My ManagedBean:

@ManagedBean(name = "product")  
@RequestScoped  
public class DataTable implements Serializable {    
    private List<Product> productsList; 

    public List<Product> getProductsList() {    
        productsList = new ArrayList<>();    
        productsList.add(new Product(1,"HP Laptop",25000f));    
        productsList.add(new Product(2,"Dell Laptop",30000f));    
        productsList.add(new Product(3,"Lenevo Laptop",28000f));    
        productsList.add(new Product(4,"Sony Laptop",28000f));    
        productsList.add(new Product(5,"Apple Laptop",90000f));    
        return productsList;  
    }    
}  

My index.xhtml (I've tried to add "rows" attribute, but I have the same problem

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:body>
    <h:form>
        <h2>
            <h:outputText value="Product List"></h:outputText>
        </h2>
        <h:dataTable value="#{product.productsList}" var="p">
            <h:column>
                <f:facet name="header">ID</f:facet>
                <h:outputText value="#{p.id}" />
            </h:column>
            <h:column>
                <f:facet name="header">Name</f:facet>
                <h:outputText value="#{p.name}" />
            </h:column>
            <h:column>
                <f:facet name="header">Price</f:facet>
                <h:outputText value="#{p.price}" />
            </h:column>
        </h:dataTable>
    </h:form>
</h:body>
</html>

This is my result:

result

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • So you added a breakpoint and the getter was reached? – Kukeltje May 26 '20 at 18:23
  • I tried your code and it works with Apache Tomcat/9.0.12, what are you using? Have you tried adding setter to your Product class? – WoAiNii May 26 '20 at 19:27
  • @WoAiNii: op should remove the table and just output `#{product.productsList}` and run in development mode... and for an [mcve] use a list of `String` and OP should read https://stackoverflow.com/questions/5765853/how-and-when-should-i-load-the-model-from-database-for-hdatatable – Kukeltje May 26 '20 at 19:59

0 Answers0