2010-04-28 58 views
0

我已經創建了一個簡單的購物車。它存儲一個項目確定,我可以返回頁面,項目仍然存在,但只要我將另一個項目添加到購物車,它會重置並只存儲該項目。有任何想法嗎?有狀態EJB不保留信息

由於

@Stateful(name="CartSessionBean") 
@Remote(CartSession.class) 
public class CartSessionBean implements CartSession, java.io.Serializable { 

    private Cart items; 

    @Init 
    public void create() { 
     items = new Cart(); 
    } 

    public void add(Book item, int qty) { 

     items.addItem(item, qty); 

    } 

Cart.java

@Entity 

@Table(名稱= 「購物車」) 公共類車實現java.io.Serializable {

@Id @GeneratedValue 
@Column(name="id") 
private int id; 

@Column(name = "total") 
private Double total; 

@OneToMany(mappedBy="cart") 
private Set<Item> items; 

public Cart(){ 
    // not valid? 

} 

//setters and getters 

public int getOrder(){ 
    return id; 
} 

public void setOrder(int order){ 
    this.id = order; 
} 

public Double getTotal(){ 
    return total; 
} 

public void setTotal(Double q){ 
    this.total = q; 
} 

public Set<Item> getItems(){ 
    return this.items; 
} 

public void setItems(Set<Item> i){ 
    this.items = i; 
} 

public void addItem(Book b, int qty) { 

    if(items == null) { 
     items = new HashSet(); 
    } 

    Item i = new Item(); 
    i.setBook(b); 
    i.setQty(qty); 

    this.items.add(i); 
} 

public void removeItem(Item id) { 
    this.items.remove(id); 
} 

public void emptyCart() { 
    this.items.clear(); 
} 

}

Servlet的

public class ServletCart extends HttpServlet { 

    private Set<Item> items; 

    @Override 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws IOException, ServletException { 

     // NO need. 

     // DO WE HAVE A REMOVE REQUEST?? 

     if (items == null) { 

      try { 
       InitialContext context = new InitialContext(); 
       CartSession cartitems = (CartSession) context.lookup("CartSessionBean/remote"); 
       items = cartitems.getItems(); 
      } catch (NamingException e) { 
       throw new ServletException("JNDI problem", e); 
      } 

     } 

     RequestDispatcher view = request.getRequestDispatcher("cart.jsp"); 
     request.setAttribute("items", items); 
     request.setAttribute("size", items.size()); 
     view.forward(request, response); 
    } 

    @Override 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws IOException, ServletException { 

     // Add a book :s/ 

     String isbn = request.getParameter("isbn"); // Does this ISBN exsit? 
     int quantity = Integer.parseInt(request.getParameter("qty")); // Is this a number ? 

     Book bk; 

     try { 
      InitialContext context = new InitialContext(); 
      BookSession guestbookSession = (BookSession) context.lookup("BookSessionBean/remote"); 
      bk = guestbookSession.getBook(isbn); 

      CartSession cartitems = (CartSession) context.lookup("CartSessionBean/remote"); 
      cartitems.add(bk, quantity); 

      this.items = cartitems.getItems(); 

     } catch (NamingException e) { 
      throw new ServletException("JNDI problem", e); 
     } 

     request.setAttribute("response", bk.getBookTitle() + " Added."); 

     doGet(request, response); 

    } 
} 
+0

購物車類是什麼樣的? 你可以在「create/init」方法中加入一些日誌代碼來查看你的購物車是否正在重新初始化?更新了 – 2010-04-28 16:14:42

+0

。謝謝 – 2010-04-28 16:15:56

+0

每次添加書籍時,都會調用create/init。導致重置。 – 2010-04-28 16:26:40

回答

0

沒有與代碼的幾個問題所呈現:

  1. 你不能在一個servlet實例變量存儲不同步。所有對servlet的請求都共享同一個實例,所以實例變量將被共享。
  2. CartSession.getItems是一個遠程方法調用,所以返回的項目列表被複制並按值傳遞。看起來你可能一直試圖解決這個問題?
  3. 每次調用context.lookup(「CartSessionBean/remote」)將創建一個有狀態會話bean的新實例。

你需要做的是這樣的:找到,或從HttpServletRequest中創建一個HttpSession,找到或創建的會話推車實例,然後用手推車會話bean工作。