2013-05-02 83 views
1

蔭性學習的有EJB3.0 + JPA(休眠)JSF數據越來越顯示在UI

蔭正從表中的所有數據,但在屏幕上它沒有顯示任何內容。 這裏是代碼。
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頁面

<?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> 
+0

調試你的豆? – Aquillo 2013-05-02 14:24:06

+0

@Aquillo這不是問題。如果你不瞭解JSF,請不要發表評論。 – 2013-05-02 14:24:48

+0

看看'h:dataTable';)你不需要自己創建表:) – Aquillo 2013-05-02 14:25:01

回答

5

的問題是,你沒有任何地方載入清單的內容。做這件事的最好的地方是在託管bean上的@PostConstruct。此外,由於你會在頁面上調用Ajax調用,它將如果您將管理bean的範圍至少爲@ViewScoped更好:

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

如果你想使用Ajax調用加載數據(因爲看起來你想/需要),那麼你忘記了更新將顯示數據的組件。您可以使用<f:ajax>render屬性來實現此目的。在這種情況下,您必須提供組件的完整ID,因爲您正在更新表單外的組件。

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

非常感謝。自2小時以來掙扎着。它的工作 – developer 2013-05-02 14:35:57