下面是我使用的cookie管理方法
public static Cookie getCookieByName(final String name) {
// Fetch all cookies from the request
Cookie[] cookies = VaadinService.getCurrentRequest().getCookies();
// Iterate to find cookie by its name
for (Cookie cookie : cookies) {
if (name.equals(cookie.getName())) {
return cookie;
}
}
return null;
}
public static Cookie createCookie(final String name, final String value, final int maxAge) {
// Create a new cookie
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(maxAge);
// Set the cookie path.
cookie.setPath(VaadinService.getCurrentRequest().getContextPath());
// Save cookie
VaadinService.getCurrentResponse().addCookie(cookie);
return cookie;
}
public static Cookie updateCookieValue(final String name, final String value) {
// Create a new cookie
Cookie cookie = getCookieByName(name);
cookie.setValue(value);
// Save cookie
VaadinService.getCurrentResponse().addCookie(cookie);
return cookie;
}
public static void destroyCookieByName(final String name) {
Cookie cookie = getCookieByName(name);
if (cookie != null) {
cookie.setValue(null);
// By setting the cookie maxAge to 0 it will deleted immediately
cookie.setMaxAge(0);
cookie.setPath("/");
VaadinService.getCurrentResponse().addCookie(cookie);
}
}我可以为cookie选择、创建和销毁cookie,但不能使用updateCookieValue(final String name, final String value)方法更新cookie的值。我在火狐和Chrome浏览器中都测试了updateCookieValue(LOCALE_COOKIE, "en");,但是浏览器的locale值并没有改变。我的方法有什么问题?
发布于 2014-08-13 16:01:58
如果您正在使用Vaadin和Push,则只能在执行UI.init()方法时更新cookies。目前还没有解决此问题的方法。
https://stackoverflow.com/questions/25278345
复制相似问题