2013-11-22 70 views
0

我有這個問題:我正在製作這個奇妙的教程The NetBeans E-commerce Tutorial。但是,不要像所介紹的那樣在JSP中創建它,而是創建一個JSF版本。只是爲了理解構建這樣的應用程序的邏輯。如何在數據呈現在jsf頁面後銷燬會話

在某些部分ControllerServlet.java,有這樣的代碼:

int orderId = orderManager.placeOrder(name, email, phone, address, cityRegion, ccNumber, cart); 

       // if order processed successfully send user to confirmation page 
       if (orderId != 0) { 

        // dissociate shopping cart from session 
        cart = null; 

        // end session 
        session.invalidate(); 

        // get order details 
        Map orderMap = orderManager.getOrderDetails(orderId); 

        // place order details in request scope 
        request.setAttribute("customer", orderMap.get("customer")); 
        request.setAttribute("products", orderMap.get("products")); 
        request.setAttribute("orderRecord", orderMap.get("orderRecord")); 
        request.setAttribute("orderedProducts", orderMap.get("orderedProducts")); 

        userPath = "/confirmation"; 

       // otherwise, send back to checkout page and display error 

正如你所看到的,筆者無效的會議,以允許其他採購訂單。我製作了一個帶有會話範圍的託管Bean,以便在整個會話期間保持數據的可用性。但是,當我嘗試清理會話時,就像作者在教程中所做的那樣,我無法收到確認數據。

然後,我創建了一個不同的託管bean,以便有一個託管bean來處理訂單(CartManagerBean),另一個託管bean提供確認(ConfirmationMBean)。我只是將confirmmatioBean注入到cartBean中以傳遞orderId,這是呈現數據所必需的。在confirmationBean中,我做了一個cleanUp()方法,使session失效。

但總是不顯示數據。所以如果有人能告訴我該怎麼做,我會感激。

這裏是數據傳遞給確認Bean的我cartBean的代碼部分:

... 
@ManagedProperty(value ="#{confirmationBean}") 
private ConfirmationMBean confirmationBean; 
... 
public String makeConfirmation() { 
    FacesContext fc = FacesContext.getCurrentInstance(); 
    if (!cartMap.isEmpty()) { 
     int orderId = orderManager.placeOrder(name, email, phone, address, credicard, cartMap); 

     // if order processed successfully send user to confirmation page 
     if (orderId != 0) { 
      // get order details 
      confirmationBean.setOrderId(orderId); 

      // dissociate shopping cart from session 
      cartMap.clear(); 

      // end session 
      //fc.getExternalContext().invalidateSession(); 
     } 
    } 

    return "confirmation"; 
} 

正如你所看到的,我評論說,會議的無效部分。這裏是我的ConfirmationMBean的實現代碼:

@ManagedBean(name = "confirmationBean") 

@SessionScoped 公共類ConfirmationMBean實現Serializable {

private Customer customer; 
private List<OrderedProduct> orderedProducts; 
private CustomerOrder orderRecord; 
private List<Product> products; 
private int orderId; 

@EJB 
private OrderManager orderManager; 

public void cleanUp(){ 
    FacesContext fc = FacesContext.getCurrentInstance(); 
    fc.getExternalContext().invalidateSession(); 
} 

private void init(){ 
    Map<String, Object> orderMap = orderManager.getOrderDetails(orderId); 

    customer = (Customer) orderMap.get("customer"); 
    orderRecord = (CustomerOrder) orderMap.get("orderRecord"); 
    orderedProducts = (List<OrderedProduct>) orderMap.get("orderedProducts"); 
    products = (List<Product>) orderMap.get("products"); 
} 

public Customer getCustomer() { 
    return customer; 
} 

public void setCustomer(Customer customer) { 
    this.customer = customer; 
} 

public List<OrderedProduct> getOrderedProducts() { 
    return orderedProducts; 
} 

public void setOrderedProducts(List<OrderedProduct> orderedProducts) { 
    this.orderedProducts = orderedProducts; 
} 

public CustomerOrder getOrderRecord() { 
    return orderRecord; 
} 

public void setOrderRecord(CustomerOrder orderRecord) { 
    this.orderRecord = orderRecord; 
} 

public List<Product> getProducts() { 
    return products; 
} 

public void setProducts(List<Product> products) { 
    this.products = products; 
} 

public int getOrderId() { 
    return orderId; 
} 

public void setOrderId(int orderId) { 
    this.orderId = orderId; 
    init(); 
    cleanUp(); 
} 

}

正如你所看到的,當將訂單被設置好的前面的bean,從數據庫中請求數據,並填充變量以顯示在facelet中。 ¿爲了獲得與本教程相同的結果,我在哪裏或者如何使用cleanUp方法?

在此先感謝。

回答

1

將bean放在請求範圍而不是會話範圍中調用操作,並獲取期望的會話作用域bean作爲(託管)屬性。

@ManagedBean 
@RequestScoped 
public class SubmitConfirmationBean { 

    @ManagedProperty("#{cartBean}") 
    private CartBean cartBean; 

    // ... 
} 

而且通過#{submitConfirmationBean.cartBean...}而不是#{cartBean...}引用它。

另外,明確提出所需的會話在同樣的動作方法的要求範圍,因爲你在哪裏會話無效範圍的bean:

externalContext.getRequestMap().put("cartBean", cartBean); 

這樣的#{cartBean...}將把請求範圍的一個,而不是會話的範圍是新創建在那一點,因爲你破壞了會議。無論如何,請求範圍的請求會被下一個請求丟失。

+0

感謝BalusC,我按照您的建議改變了代碼,但這種方式我沒有找到發送orderId的方法,它將所有數據顯示在確認頁面中。我得到了一個N​​ullPointerException,我必須獲取orderId值來從數據庫請求數據。我還在工作,如果我找到解決方案,我會在這裏發佈 –

+0

也許你改變太多了?它只應用於** **提交按鈕,使會話無效,而不是所有其他方法/視圖!另外,我無法確定在最後一頁顯示哪個bean時,在無效期間。這也很可能意味着你保留'ConfirmationBean'而不是'CartBean'。你的問題不清楚。 – BalusC

+0

沒錯,我想保留'ConfirmationBean',但這個bean使用'CartBean'中生成的orderId值。那麼,我怎樣才能將這個值從一個bean傳遞給另一個呢? –