Vaadin 7:如何更新Cookie值? [英] Vaadin 7 : How to update Cookie value?
问题描述
我使用的cookie管理方法如下
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);
}
}
我可以选择,创建和销毁Cookie,但我无法使用我的方法 updateCookieValue(最终字符串名称,最终字符串值)
更新cookie的值。我在Firefox和Chrome浏览器中都测试了 updateCookieValue(LOCALE_COOKIE, en);
,但是浏览器的语言环境值没有变化。我的方法有什么问题?
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 ?
推荐答案
如果您将Vaadin与Push一起使用,则只能在执行UI.init()时更新cookie。方法。当前没有解决此问题的方法。
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
这篇关于Vaadin 7:如何更新Cookie值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!