developer
developer

Reputation: 9478

Data is getting displayed on the UI

Iam learing JSF with EJB3.0 + JPA(hibernate)

Iam getting all data from the tables but on screen it is not displaying anything. here is the code.
Managed Bean

package retail.web.mbean;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.faces.bean.ManagedBean;
import javax.faces.event.AjaxBehaviorEvent;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import retail.ejb.service.OrderSessionBeanRemote;
import retail.model.vo.Customer;
import retail.model.vo.Order;
import retail.model.vo.Products;

@ManagedBean
public class OrdersMB {

    private Order order = new Order();

    private HashMap<Integer,String> customerMap = new HashMap<Integer,String>();

    private HashMap<Integer,String> productsMap = new HashMap<Integer,String>();

    private String customerName;

    private String productName;

    private List<Order> orderList;

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }


    public HashMap<Integer, String> getCustomerMap() {
        return customerMap;
    }

    public void setCustomerMap(HashMap<Integer, String> customerMap) {
        this.customerMap = customerMap;
    }

    public HashMap<Integer,String> getProductsMap() {
        return productsMap;
    }

    public void setProductsMap(HashMap<Integer, String> productsMap) {
        this.productsMap = productsMap;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }



    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public List<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }

    public void getOrderLists() throws NamingException{
        Properties p = new Properties();
        //p.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");
        p.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.impl.SerialInitContextFactory");
        p.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
        p.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
        p.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
        p.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); //any configured port different from 3700 - 34513
        InitialContext c = new InitialContext(p);
        OrderSessionBeanRemote remote = (OrderSessionBeanRemote) c.lookup("java:global/RetailProducts/OrderSessionBeanImpl!retail.ejb.service.OrderSessionBeanRemote");
         List<Order> orderList = remote.getOrderLists();
         setOrderList(orderList);


    }
public void deleteOrder(Order order) throws NamingException{
    Properties p = new Properties();
    //p.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");
    p.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.impl.SerialInitContextFactory");
    p.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
    p.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
    p.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
    p.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); //any configured port different from 3700 - 34513
    InitialContext c = new InitialContext(p);
    OrderSessionBeanRemote remote = (OrderSessionBeanRemote) c.lookup("java:global/RetailProducts/OrderSessionBeanImpl!retail.ejb.service.OrderSessionBeanRemote");
    remote.deleteOrder(order);
}

}


xhtml page

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>List of Orders</title>

    <script>
        window.onload = function() {
            document.getElementById('hidden:link').onclick();
        }
    </script>
        <h:outputStylesheet library="css" name="table-style.css"  />
    </h:head>
    <h:body>
   <h:form id="hidden" style="display:none">
        <h:commandLink id="link">
        <f:ajax event="click" listener="#{order.getOrderLists}"/>

        </h:commandLink>
    </h:form>
        <h1 style="background-color:#4863A0;color:white;">List of Orders</h1>
        <f:view>
 <h:form>
        <table class="order-table">
            <tr>
                <th class="order-table-header">Order #</th>
                <th class="order-table-header">Customer</th>
                <th class="order-table-header">Item</th>
                <th class="order-table-header">Action</th>
            </tr>
            <tbody>
                <ui:repeat var="o" value="#{order.orderList}" >

                  <tr>
                        <td class="order-table-even-row">#{o.orderID}</td>
                        <td class="order-table-even-row">#{o.customerName}</td>
                        <td class="order-table-even-row">#{o.productTile}</td>
                        <td class="order-table-even-row"><h:commandLink value="Delete" action="#{order.deleteOrder(o)}" /></td> 
                      </tr>

                </ui:repeat>
            </tbody>
        </table>
        </h:form>
        <table >

<tr><td>Total Customers: 1</td>
</tr>
</table>
<div style=" border-bottom: 5px ridge blue;"> </div> 

<table>
<tr>
<td><h:outputLink  value="createbook.xhtml">Create a new Order</h:outputLink>|</td><td><h:outputLink  value="eBusiness.jsp">Main Page</h:outputLink></td>
</tr>
</table>
</f:view>
    </h:body>
</html>

Upvotes: 1

Views: 102

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

The problem is that you're not loading your list content anywhere. The best place for doing this is in a @PostConstruct on your managed bean. Also, since you will invoke ajax calls on your page, it would be better if you set the scope of your managed bean at least as @ViewScoped:

@ManagedBean
@ViewScoped
public class OrdersMB {
    //attributes, methods, getters, setters...
    @PostConstruct
    public void init() {
        try {
            getOrderLists();
        } catch (NamingException ne) {
            //handle your error here...
        }
    }
}

In case you want to load the data using an ajax call (as it looks you want/need), then you forgot to update the component that will display the data. You can achieve this using the render attribute of <f:ajax>. In this case, you must provide the full id of the component since you're updating a component outside the form.

<h:form id="hidden" style="display:none">
    <h:commandLink id="link">
    <f:ajax event="click" listener="#{order.getOrderLists}"
        render=":frmData" />
    </h:commandLink>
</h:form>
<!-- some HTML/JSF content here... -->
<h:form id="frmData">
</h:form>

Upvotes: 5

Related Questions