2013-05-02 89 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