问题
Methods for cookie management these I use as below
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);
}
}
I can select,create and destroy for cookies but I can't update cookie's value with my method updateCookieValue(final String name, final String value)
. I tested in both Firefox and Chrome browser as updateCookieValue(LOCALE_COOKIE, "en");
but locale value of browsers didn't change. What's wrong with my method ?
回答1:
If you are using Vaadin with Push you can only update cookies while executing UI.init() method. There is currently no workaround for this problem.
http://dev.vaadin.com/ticket/11808
来源:https://stackoverflow.com/questions/25278345/vaadin-7-how-to-update-cookie-value