JSF 2.0:Ajax呼叫后不立即持续cookie,需要http请求
-
12-10-2019 - |
题
为了从购物车中删除物品而进行的Ajax呼叫 - removeOrder()
方法被调用
UI removeOrder()
致电(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()
致电(托管豆)
public void removeOrder(ActionEvent e) {
String productName = (String) e.getComponent().getAttributes().get("remove");
Product p = getProductByName(productName);
inCart.remove(p);
persistCookies();
emptyCartNotifier();
totalRendered();
}
在这里,cookie持续存在,如预期的那样,该方法的输出,cookie数组包含具有空值的cookie,没关系:
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()请参阅 非空的“先前”饼干 大批
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致电清洁饼干后, HttpServletRequest
包含非空的cookie,直到执行新的HTTP请求为止(用户提交按钮或按链接使用)。
当Web-App结合Ajax和非AJAX调用时,是否有解决方案或良好的练习?
谢谢你。
解决方案
等等,什么?在 emptyCartNotifier
, ,你在打电话 getCookies
在 HttpRequest
对象,应该包含cookie 在HTTP请求中 这触发了您的方法,因此,直到下一个请求,您当然不会看到更改。
其他提示
您的Cookie问题是一回事,但我认为有一种更简单的方法来完成工作。
在您的页面中,只需替换您提到的页面:
<p:commandButton value="clean" action="#{showProducts.removeOrder(cart)}"
process="@form" update="@form,:ccId:cCart:ccSizeId,:ccId:cCart:ccTotId" immediate="true" />
支持bean(例如,范围):
private double subtotal; // don't forget getter and setter
public void removeOrder(Product product) {
inCart.remove(product);
// calculate subtotal
}
我想您显示了小计,也许在打电话后列出所有产品 删除订单. 。删除产品后只需重新计算小计,并确保购物车得到刷新(更新 属性)。
该代码足够简单且易于理解,但是您需要的一切。无需“手动”管理cookie。
您可以尝试设置饼干 setHttpOnly(true)
, ,但问题是为什么您根本需要“ Ajax分割”的Cookie持久性?
为什么不在您的视图/请求/会话范围内使用本地变量?它们实际上是为这类任务而设计的。如果您想要其他cookie持久性,则在相应的设置器或操作方法中进行操作。