0
votes

I have this problem: I´m making this wonderfull tutorial The NetBeans E-commerce Tutorial . But instead of make it in JSP as is presented, i´m making a JSF version. Just to undertands the logic in construction an application like that.

In certain part the ControllerServlet.java, has this code:

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

As you can see, the author invalidates the session, in order to permit another purchase order. I made an Managed Bean with session scope in order to mantain the data avalaible throught the whole session. But when I try to clean up the session, as in the tutorial the author does, I can´t receive the data for confirmation.

Then, I made a different managed bean in order to have one to process the order (CartManagerBean), and another one to present the confirmation (ConfirmationMBean). I just injected the confirmatioBean into the cartBean to pass the orderId, necessary to present the data. In the confirmationBean, I made a cleanUp() method that invalidates the session.

But always, the data is not presented. So if any one can tell me what to do, I´ll appreciate.

Here is the part of my cartBean's code that pass the data to the confirmation bean:

...
@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";
}

As you can see, I commented the part that invalidates the session. Here is the code that I implemented for the ConfirmationMBean:

@ManagedBean(name = "confirmationBean")

@SessionScoped public class ConfirmationMBean implements 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();
}

}

As you can see, when the orderId is setted by the preceding bean, the data is requested from the database, and populates the variables to present in the facelet. ¿Where or how I have to use the cleanUp method in order to obtain the same result that the tutorial?

Thanks in advance.

1

1 Answers

1
votes

Put the bean where you're invoking the action in the request scope instead of session scope and get hold of the desired session scoped bean as a (managed) property.

@ManagedBean
@RequestScoped
public class SubmitConfirmationBean {

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

    // ...
}

And reference it by #{submitConfirmationBean.cartBean...} instead of #{cartBean...}.

Alternatively, explicitly put the desired session scoped bean in the request scope in the same action method as where you're invalidating the session:

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

This way the #{cartBean...} will refer the request scoped one instead of the session scoped one which is newly recreated at that point because you destroyed the session. The request scoped one is lost by next request anyway.