我正在开发一个销售汽车零件的Java EE电子商务应用程序。我可以将项目添加到使用CopyOnWriteArrayList实现的购物车中。然后我使用托管bean(会话作用域)来显示其内容。 然后我用a来显示ViewCode.xhtml中的数据。问题是:它只会显示arraylist中的第一个项目。请有人帮忙吗? Managed Bean:
@ManagedBean(name="ViewCartManagedBean")
@SessionScoped
public class ViewCartManagedBean {
List<Orderitem> cartItemsList = new ArrayList<>();
@EJB
com.shop.cart.CartFacade cartFacade;
public ViewCartManagedBean(){}
@PostConstruct
public void initialize(){
setCartItemsList();
}
public List<Orderitem> getCartItemsList() {
return cartItemsList;
}
public void setCartItemsList() {
cartItemsList = cartFacade.viewItemsIntheCart();
}
}
购物车门面
@Stateless(name="ShoppingCartLocal")
public class CartFacade {
@PersistenceContext(unitName ="Shop")
EntityManager cartEntityManager;
@EJB
ShoppingCart cartBean;
public CartFacade(){}
public void assignCustomerToCart(CustomerEntity customer){
cartBean.setCustomer(customer);
}
public boolean addItemToCart(ProductEntity product, int quantityPurchased){
boolean result = false;
result=cartBean.addCartItem(product, quantityPurchased);
return result;
}
public List<Orderitem> viewItemsIntheCart(){
return cartBean.getCartItems();
}
the ShoppingCartBean
@Stateful(name="ShoppingCart")
@StatefulTimeout(value = 200)
public class ShoppingCartBean implements ShoppingCart,ShoppingCartLocal {
@PersistenceContext(unitName ="Shop", type=PersistenceContextType.EXTENDED)
private EntityManager cartEntityManager;
private CustomerManager customerManagerBean;
private CopyOnWriteArrayList<Orderitem> cartItems= new CopyOnWriteArrayList<Orderitem>();
private CustomerEntity customer=null;
public CustomerEntity getCustomer() {
return customer;
}
public void setCustomer(CustomerEntity customer) {
this.customer = customer;
System.out.println("hey Im linked to the cart " + customer.getName());
}
private com.shop.entity.Orderitem basketItem=new Orderitem();
public ShoppingCartBean(){}
@PostConstruct
public void initialise(){
//cartItems = new CopyOnWriteArrayList<Orderitem>();
}
public void removeCartItem(int itemCode){
System.out.println("hello");
}
@Override
public boolean addCartItem(ProductEntity product, int quantityPurchased){
double subTotal;
boolean result=false;
if (cartItems.size()==0){
basketItem.setItemcode(product.getItemcode());
basketItem.setItemdescription(product.getItemdescription());
basketItem.setUnitprice(product.getUnitprice());
basketItem.setQuantitypurchased(quantityPurchased);
subTotal=quantityPurchased*basketItem.getUnitprice();
basketItem.setSubtotal(subTotal);
cartItems.add(basketItem);
result=true;
}else{
Iterator<Orderitem> basketIterator = cartItems.iterator();
while (basketIterator.hasNext()){
Orderitem cartItem = basketIterator.next();
System.out.println("cart item item code: " + cartItem.getItemcode());
System.out.println("product item code: " + product.getItemcode());
if(cartItems.size()!=0 && !(cartItem.getItemcode().equals((product.getItemcode())))){
basketItem.setItemcode(product.getItemcode());
basketItem.setItemdescription(product.getItemdescription());
basketItem.setUnitprice(product.getUnitprice());
basketItem.setQuantitypurchased(quantityPurchased);
subTotal=quantityPurchased*basketItem.getUnitprice();
basketItem.setSubtotal(subTotal);
cartItems.add(basketItem);
result=true;
}
}
}
System.out.println("cart size " + cartItems.size());
return result;
}
最后是视图(ViewCart.xhtml)
<h:form name="ViewCartManagedBean">
<h:dataTable var="cartList" value="#{ViewCartManagedBean.cartItemsList}"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row" >
<h:column>
<f:facet name="header"><h:outputText value="Item Code" style="font-family: Verdana, Helvetica, sans-serif;font-size: 16px;"/></f:facet>
<h:outputText value="#{cartList.itemcode}" style="font-family: Verdana, Helvetica, sans-serif;font-size: 14px;"/>
</h:column>
答案 0 :(得分:1)
数据表看起来像是显示列表数据的有效方式,几乎是tutorial的副本
但是,添加数据的方式是错误的。 (这就是我建议在评论中使用调试器的原因。)
在打印时,您应该只注意到列表中的1个元素(我期望)或同一个项目的多次。
您的实例变量:
private com.shop.entity.Orderitem basketItem=new Orderitem();
一遍又一遍地重复使用。这应该是方法变量而不是实例变量。您一遍又一遍地将相同的实例添加到列表中,同时更新它的属性。这将更新单个实例的属性,而不是创建新实例。这意味着,如果你这样做:
List list = new SomeList();
SomeObject object = new SomeObject();
list.add(object);
list.add(object);
object.setName("name");
该列表将包含对同一对象的2个引用,两个名称均为“name”
再次:这是您在使用实例变量时所做的事情 basketItem而不是使它成为仅存在于方法addCartItem
中的变量将商品添加到购物车时,您应使用以下内容:
public boolean addCartItem(ProductEntity product, int quantityPurchased){
for (Orderitem current : cartItems) {
if (current.getItemCode().equals(product.getItemCode())) {
// adding duplicate entry.
return false; // or handle adding differently
}
}
// now that we're sure that the item doesn't already exist in the basket
Orderitem basketItem=new Orderitem();
basketItem.setItemcode(product.getItemcode());
basketItem.setItemdescription(product.getItemdescription());
basketItem.setUnitprice(product.getUnitprice());
basketItem.setQuantitypurchased(quantityPurchased);
basketItem.setSubtotal(quantityPurchased*basketItem.getUnitprice());
cartItems.add(basketItem);
return true;
}
下次运行调试器并检查(在向列表中添加多个项目之后)列表项的实际内容是什么。这样你就可以发现出现了问题。