0

I'm learning Web/J2EE with EJB/JPA/JSF, and so I followed a simple tutorial for NetBeans 7.3/Glassfish 3.1.2.

So I simply created a new Java Enterprise Application, entity classes from sample database (Customer), a session bean CustomerManager, and in the web project a JSFManagedBean and a JSF Page.

In the body of my JSF page I added just after <h1>Clients list</h1> a JSF Data Table From Entity (with NetBeans palette). And here is my problem : no data is print on the page after "Clients list". But when I go in Services>Databases>sample, I perfectly see all the entries.

I have no idea of what is wrong... thanks for your help.

Edit : here is my code.

In EJB, CustomerManager.java :

@Stateless
@LocalBean
public class CustomerManager {

    @PersistenceContext(unitName = "CustomerApplication-ejbPU")
    private EntityManager em;

    public List<Customer> getAllCustomers() {
        Query q = em.createNamedQuery("Customer.findAll");
        return q.getResultList();
    }

    public Customer update(Customer c) {
        return em.merge(c);
    }

    public void persist(Object o) {
        em.persist(o);
    }
}

Web : here is my code. CustomerMBean :

@ManagedBean
@ViewScoped
public class CustomerMBean {

    private List<Customer> customerList;

    @EJB
    private CustomerManager manager;

    public CustomerMBean() {
    }

    public List<Customer> getAllCustomers() {
        return manager.getAllCustomers();
    }

    public String showDetails(int idCustomer) {
        return "CustomerDetails?idCustomer=" + idCustomer;
    }
}

And my CustomerList page :

    <h:body>
        <h1>Liste des clients</h1>
        <f:view>
            <h:form>
                <h1><h:outputText value="List"/></h1>
                <h:dataTable value="#{customerMBean.allCustomers}" var="item">
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="CustomerId"/>
                        </f:facet>
                        <h:outputText value="#{item.customerId}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Name"/>
                        </f:facet>
                        <h:outputText value="#{item.name}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Addressline1"/>
                        </f:facet>
                        <h:outputText value="#{item.addressline1}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Addressline2"/>
                        </f:facet>
                        <h:outputText value="#{item.addressline2}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="City"/>
                        </f:facet>
                        <h:outputText value="#{item.city}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="State"/>
                        </f:facet>
                        <h:outputText value="#{item.state}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Phone"/>
                        </f:facet>
                        <h:outputText value="#{item.phone}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Fax"/>
                        </f:facet>
                        <h:outputText value="#{item.fax}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Email"/>
                        </f:facet>
                        <h:outputText value="#{item.email}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="CreditLimit"/>
                        </f:facet>
                        <h:outputText value="#{item.creditLimit}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Zip"/>
                        </f:facet>
                        <h:outputText value="#{item.zip}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="DiscountCode"/>
                        </f:facet>
                        <h:outputText value="#{item.discountCode}"/>
                    </h:column>
                </h:dataTable>
            </h:form>
        </f:view>
</h:body>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rob
  • 15,732
  • 22
  • 69
  • 107
  • Your approach, even if trivial, seems correct. Please show the Customer entity class. Go under the Output tab of Netbeans and look at the GF server log...did u see anything? – elbuild Jan 18 '14 at 20:54
  • I looked at it, nothing seemed to be relevant about my problem :/ – Rob Jan 18 '14 at 23:07
  • 1
    Apart from the concrete problem, performing business logic in a getter method is a bad practice. See also http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times/2090062#2090062 – BalusC Jan 19 '14 at 16:32

1 Answers1

-1

Try this it may helpfull for you.

<h:dataTable value="#{customerMBean.customerList}" var="item">

in place of

<h:dataTable value="#{customerMBean.allCustomers}" var="item">

For Balacus

 <h:dataTable value="#{employeeMBean.customerList}" var="item">

and ManageBean is

@ManagedBean
@ViewScoped
public class CustomerMBean {

    private List<Customer> customerList;

    @EJB
    private CustomerManager manager;

    public CustomerMBean() {
    }
    @PostConstruct
    public void getAllCustomers() {
        customerList= manager.findAll();
    }
      //setter getter for customer list
}
sumit sharma
  • 1,067
  • 8
  • 24
  • What a nonsense. You've clearly never really written some working JSF code from top of head. Please stop posting wild guesses as answers. Better use comments for that. – BalusC Jan 19 '14 at 16:30
  • @BalausC - I give the replacement that he can also try using variable but it is obvious to initialize the variable for you I am editing the above code and tell me what is wrong with it. – sumit sharma Jan 23 '14 at 08:46