执行 Ajax 调用以从购物车中删除商品 -removeOrder()
调用方法
UIremoveOrder()
调用(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>
后端removeOrder()
调用(托管 bean)
public void removeOrder(ActionEvent e) {
String productName = (String) e.getComponent().getAttributes().get("remove");
Product p = getProductByName(productName);
inCart.remove(p);
persistCookies();
emptyCartNotifier();
totalRendered();
}
这里cookies是持久化的,这个方法的输出和预期的一样,cookie数组包含空值的cookies,没关系:
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);
}
这里出现了问题,方法 emptyCartNotifier() 看到非空的“previous” Cookies数组
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;
}
在执行任何 HTTP 请求之后,该 Cookie 数组就真正被清除了。
如我所见,冲突是:
在 AJAX 调用清除 cookie 之后,该 cookieHttpServletRequest
包含非空 cookie,直到执行新的 HTTP 请求(用户提交按钮或通过链接访问)。
当网络应用程序结合 AJAX 和非 AJAX 调用时,是否有即时 cookie 管理的解决方案或良好做法?
谢谢你。