购物车项目为空



-我正在IntelliJ上使用WildFly 9.0 PostGresql、JSF、托管bean、实体bean等开发一个Java EE电子商务应用程序。-用户可以点击一个类别,与该类别相关的产品将显示在PrimeFaces网格中。-所选产品及其数量可以添加到ShoppingCartBean.java中的列表中(CopyOnWriteArrayList cartItemList;(-上面的bean实例被注入(使用@EJB(到CartFacade.java中,后者被注入到托管bean中。-没有记录错误,系统没有崩溃部署完美,将所选产品添加到cartItemList中。-调用ViewCart.xhtml时,arraylist将重置为空。三天来,我一直在努力解决这个问题。请有人帮忙。感谢

这是代码。第一个:ShoppingCartBeanJava(为简洁起见,删除了导入(。这实现了一个远程接口和一个本地接口。

@Stateful(name="ShoppingCart")
public class ShoppingCartBean implements ShoppingCart, ShoppingCartLocal {
@PersistenceContext(unitName ="Shop", type=PersistenceContextType.EXTENDED)

   private EntityManager cartEntityManager;
    private CustomerManager customerManagerBean;
    private CopyOnWriteArrayList<Orderitem> cartItems = new CopyOnWriteArrayList<>();
    private  CustomerEntity customer=null;

    public ShoppingCartBean(){}

    @PostConstruct
    private void initialize(){
    }

    @Override
    public boolean addCartItem(ProductEntity product, int quantityPurchased){
        com.shop.entity.Orderitem basketItem=new Orderitem();
        Locale currentLocale = new Locale("en", "GB");
        double subTotal;
        Boolean returnVal=false;
        if(cartItems.size()!=0) {
            // check for duplicate entry.
            for (Orderitem itemsIntheCart : cartItems) {
                if (itemsIntheCart.getItemcode().equals(product.getItemcode())) {
                    returnVal = false;
                }
            }
        }
        // now that we're sure that the item doesn't already exist in the basket
        // lets add the Orderitem to the cart
        basketItem.setItemcode(product.getItemcode());
        basketItem.setItemdescription(product.getItemdescription());
        basketItem.setUnitprice(product.getUnitprice());
        basketItem.setQuantitypurchased(quantityPurchased);
        subTotal=quantityPurchased*basketItem.getUnitprice();
        Double currencyAmount = new Double(subTotal);
        NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
        currencyFormatter.format(currencyAmount);
        basketItem.setSubtotal(currencyAmount);
        synchronized (cartItems) {
            cartItems.add(basketItem);
        }
        //syncCart =  java.util.Collections.synchronizedList(cart);
        System.out.println("just added an item to cartitemslIst " + cartItems.size());

        if(cartItems!=null){
            returnVal= true;
        }
        return returnVal;
    }

@Override
    public List<Orderitem> viewCartItems(){
        return getCartItems();
    }
/***********This method is accessed by the CartFacade.java See below*****/
    @Override
    public List<Orderitem> getCartItems(){
        CopyOnWriteArrayList<Orderitem> cartItemList;// = new CopyOnWriteArrayList<Orderitem>();
        cartItemList= this.cartItems;
        System.out.println("inside get cart items " + this.cartItems.size()); // this returns zero!!!!
        return cartItemList;
    }
/***********************end of offending method**********************/
}

然后推车正面

Stateful(name="ShoppingCartLocal")
@SessionScoped
public class CartFacade {
    private CopyOnWriteArrayList<Orderitem>myCartList ;
    @PersistenceContext(unitName ="Shop")
    EntityManager cartEntityManager;
    @EJB
    ShoppingCart cartBean;//= new ShoppingCartBean();
    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);
        System.out.println("Result from cart facade " + result);
        return result;
    }
    public List<Orderitem> viewItemsIntheCart(){
        CopyOnWriteArrayList<Orderitem> myCartList;
        //this stops the inner cart from getting returned to the front
        myCartList= (CopyOnWriteArrayList<Orderitem>) cartBean.getCartItems();
        return myCartList;
    }

然后在托管Bean 内部

@ManagedBean(name="ViewCartManagedBean")
@SessionScoped
public class ViewCartManagedBean {
    CopyOnWriteArrayList<Orderitem> cartItemsList = new CopyOnWriteArrayList<Orderitem>();
    @EJB
    com.shop.cart.CartFacade cartFacade;
    private double cartTotal;
    public double getCartTotal() {
        return cartTotal;
    }

    public ViewCartManagedBean(){}
    @PostConstruct
    public void initialize(){
        setCartItemsList();
    }
    public List<Orderitem> getCartItemsList() {
        //setCartItemsList();
        return cartItemsList;
    }
    public void setCartItemsList() {
        cartItemsList=null;
        cartItemsList = (CopyOnWriteArrayList<Orderitem>) cartFacade.viewItemsIntheCart();
    }

最后是xhtml页面(ViewCart.xhtml(

    <body>
<h:outputText value="Cart List"
              style="font-family: Verdana, Helvetica, sans-serif;font-size: 18px;  font-weight: 900;" />
<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>
        <h:column>
            <f:facet name="header">
                <h:outputText value="Description"
                              style="font-family: Verdana, Helvetica, sans-serif;font-size: 16px;" />
            </f:facet>
            <h:outputText value="#{cartList.itemdescription}"
                          style="font-family: Verdana, Helvetica, sans-serif;font-size: 14px;" />
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputText value="Quanitity"
                              style="font-family: Verdana, Helvetica, sans-serif;font-size: 16px;" />
            </f:facet>
            <h:outputText value="#{cartList.quantitypurchased}"
                          style="font-family: Verdana, Helvetica, sans-serif;font-size: 14px;" />
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputText value="Unit Price"
                              style="font-family: Verdana, Helvetica, sans-serif;font-size: 16px;" />
            </f:facet>
            <h:outputText value="#{cartList.unitprice}"
                          style="font-family: Verdana, Helvetica, sans-serif;font-size: 14px;">
                <f:convertNumber currencySymbol="£" groupingUsed="true"
                                 maxFractionDigits="2" type="currency" />
            </h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputText value="Sub Total"
                              style="font-family: Verdana, Helvetica, sans-serif;font-size: 16px;" />
            </f:facet>
            <h:outputText value="#{cartList.subtotal}"
                          style="font-family: Verdana, Helvetica, sans-serif;font-size: 14px;"
                          converter="javax.faces.BigDecimal">
                <f:convertNumber currencySymbol="£" groupingUsed="true"
                                 maxFractionDigits="2" type="currency" />
            </h:outputText>
        </h:column>
    </h:dataTable>
    <f:facet name="header">
        <h:outputText value="Grand Total"
                      style="font-family: Verdana, Helvetica, sans-serif;font-size: 16px;" />
    </f:facet>
    <h:outputText value="#{ViewCartManagedBean.cartTotal}"
                  style="font-family: Verdana, Helvetica, sans-serif;font-size: 14px;"
                  converter="javax.faces.BigDecimal">
        <f:convertNumber currencySymbol="£" groupingUsed="true"
                         maxFractionDigits="2" type="currency" />
    </h:outputText>
</h:form>
<h:link value="Continue Shopping" outcome="Products" />
</body>

您在使用远程bean吗?如果不是,我会去掉Stateful注释,只在bean和facade上使用javax.enterprise.context.SessionScoped。

相关内容

  • 没有找到相关文章

最新更新