Iam使用EJB3.0 + JPA(hibernate)学习JSF
我从表中获取所有数据但在屏幕上却没有显示任何内容。
这是代码。
托管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 :(得分: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调用加载数据(因为它看起来您想要/需要),那么您忘记更新将显示数据的组件。您可以使用render
的{{1}}属性来实现此目的。在这种情况下,您必须提供组件的完整ID,因为您正在更新表单之外的组件。
<f:ajax>