Ajax call performed in order to remove item from shopping cart - removeOrder()
method is called
UI removeOrder()
call(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()
call(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();
}
Here cookies is persisted,output of this method as is expected,Cookie array contains cookies with empty values,that's 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);
}
Here problem occurred, the method emptyCartNotifier() see non-empty "previous" 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;
}
After any HTTP request performed, that Cookie array is really cleaned up.
As I see , clash is:
after AJAX call cleans cookie, that HttpServletRequest
contains non-empty cookie until new HTTP request performed(user submit button or go by link).
Is there solution or good practice for immediate cookie management,when web-app combines AJAX and non-AJAX calls?
Thank you.
Wait, what? In emptyCartNotifier
, You are calling getCookies
on the HttpRequest
object, which is supposed to contain the cookies that were in the HTTP request that triggered your method, so of course you don't see changes until the next request.