Frage

ausgeführt Ajax-Aufruf zu entfernen, um Artikel aus dem Warenkorb - removeOrder() Methode wird aufgerufen,

UI removeOrder() Aufruf (JSF & Primefaces):

<p:commandButton value="clean" actionListener="#{showProducts.removeOrder}"
   process="@form" update="@form,:ccId:cCart:ccSizeId,:ccId:cCart:ccTotId" immediate="true">
<f:attribute name="remove" value="#{cart.name}"/>
</p:commandButton>

Backend removeOrder() Aufruf (Managed Bean)

public void removeOrder(ActionEvent e) {
        String productName = (String) e.getComponent().getAttributes().get("remove");
        Product p = getProductByName(productName);
        inCart.remove(p);
        persistCookies();
        emptyCartNotifier();
        totalRendered();
    }

Hier Cookies beibehalten wird, Ausgang dieses Verfahrens wie erwartet, Cookie-Array enthält Cookies mit leeren Werten, das ist OK:

private void persistCookies() {
    HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

    String ids = "";

    for (Product prod : inCart) {
        // TODO change logic to support real count,for now 1 is available only
        ids += prod.getId() + ";" + prod.getCount() + "_";
    }

    Cookie cookie = new Cookie(SC_COOKIE, ids);
    Cookie cookie2 = new Cookie(SC_SIZE, String.valueOf(inCart.size()));
    Cookie cookie3 = new Cookie(SC_TOTAL_PRICE, String.valueOf(subTotal));
    cookie3.setPath("/");
    cookie3.setMaxAge(TWO_WEEKS);
    httpServletResponse.addCookie(cookie3);
    cookie.setPath("/");
    cookie.setMaxAge(TWO_WEEKS);
    cookie2.setPath("/");
    cookie2.setMaxAge(TWO_WEEKS);

    httpServletResponse.addCookie(cookie);
    httpServletResponse.addCookie(cookie2);

}

Hier Problem aufgetreten ist, wobei das Verfahren emptyCartNotifier () finden Sie unter nicht leer "vorherige" Cookies array

private String emptyCartNotifier() {

    HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    Cookie[] cookies = httpServletRequest.getCookies();
    boolean isCookiePresent = false;
    if (cookies != null) {
        for (Cookie c : cookies) {
            if (SC_COOKIE.equals(c.getName()) && (!c.getValue().isEmpty())) {
                isCookiePresent = true;
            }
        }
    }
    if (inCart.isEmpty() && (!isCookiePresent)) {
        emptyCartNotifier = "you cart is empty";
        isFormRendered = false;
    } else {
        emptyCartNotifier = "";
        isFormRendered = true;
    }
    return emptyCartNotifier;
}

Nach jeder HTTP-Anfrage durchgeführt, dass Cookie-Array wirklich aufgeräumt wird.

Wie ich sehe, Zusammenstoß ist.
nach AJAX-Aufruf säubert Cookie, dass HttpServletRequest enthält nicht leere Cookie, bis neue HTTP-Anforderung ausgeführt (Benutzer Submit-Button oder Link gehen)

Gibt es Lösung oder eine gute Praxis für die sofortige Cookie-Management, wenn Web-App kombiniert AJAX und nicht-AJAX Anrufe?

Danke.

War es hilfreich?

Lösung

Bitte warte, was? In emptyCartNotifier, Sie rufen getCookies auf dem HttpRequest-Objekt, das die Cookies enthalten sollte, dass in der HTTP-Anforderung wurden , die Ihre Methode ausgelöst, so natürlich Sie nicht sehen, Änderungen bis zur nächsten Anforderung .

Andere Tipps

Your cookie problem is one thing, but I think there is a much easier way to get things done.

In your page, just replace the pages you mention with this:

<p:commandButton value="clean" action="#{showProducts.removeOrder(cart)}"
   process="@form" update="@form,:ccId:cCart:ccSizeId,:ccId:cCart:ccTotId" immediate="true" />

Backing bean (e.g. session scoped):

private double subtotal; // don't forget getter and setter

public void removeOrder(Product product) {
        inCart.remove(product);
        // calculate subtotal
}

I guess you show the subtotal and maybe list all products after calling removeOrder. Just recalculate subtotal after removing the product, and make sure the shopping cart is refreshed (update attribute).

This code is simple enough and easy to understand, and yet does everything you need. No need to "manually" manage cookies.

You could try setting your cookies setHttpOnly(true), but the question is why would you need "ajax-scoped" cookie persistence at all?

Why not use local variables in your view/request/session scoped beans? They're actually designed for that sort of tasks. If you want additional cookie persistence you do it in corresponding setters or action methods.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top