0

I am trying to print a simple dataTable. I have made two classes Product and ProductController, and i am passing a List to but Whenever i run the code, it only prints the dataTable column header values but not the data that was supposed to be fetched from List.

I am using Wildfly 10.0.1 server


index.xhtml code

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

Product Class

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 Class

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

}

output

ps: this was youtube video code but its not running on my machine,.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Irum
  • 65
  • 9
  • Please take a **good** tutorial and please run your application in jsf development mode if you are developing (read https://stackoverflow.com/tags/jsf/info) – Kukeltje Sep 19 '17 at 21:03
  • it looked like a good tutorial as code was running smoothly at the end of that video. – Irum Sep 19 '17 at 21:05
  • Youtube videos are not always good tutorials (hard to copy/paste)... use ones mentioned at the link I posted. You have a major 'problem' with the ManageBean annotation (package wise)'. The tutorial is about an older version of JSF, you are using a newer one. Also check the namespaces and JSF managed beans vs CDI ones... You do not want to immediately introduce 'legacy' code. – Kukeltje Sep 19 '17 at 21:07
  • Did you do a 'view' source on the client-side and looked what **is** happening there? Tried other JSF components? Do they work? – Kukeltje Sep 19 '17 at 21:26
  • I tried, but they also dont work – Irum Sep 19 '17 at 21:32
  • 1
    @Kukeltje The border="1" missing in screenshot is sufficient confirmation. – BalusC Sep 19 '17 at 21:33
  • @BalusC: good catch of this detail. I indeed missed that. Too late/tired working on my old 'xforms' based jsf xml forms solution. Might need to (want to) 'officially' hire you for a day in the maybe not too distant future to give some advice. – Kukeltje Sep 19 '17 at 21:43
  • @BalusC can you please tell me with which question my question resembles? – Irum Sep 20 '17 at 04:51
  • Look in top of your question. – BalusC Sep 20 '17 at 06:04

0 Answers0