首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vaadin 7:如何更新Cookie值?

问Vaadin 7:如何更新Cookie值?
EN

Stack Overflow用户
提问于 2014-08-13 12:55:36
回答 1查看 1.8K关注 0票数 0

下面是我使用的cookie管理方法

代码语言:javascript
复制
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值并没有改变。我的方法有什么问题?

EN

回答 1

Stack Overflow用户

发布于 2014-08-13 16:01:58

如果您正在使用Vaadin和Push,则只能在执行UI.init()方法时更新cookies。目前还没有解决此问题的方法。

http://dev.vaadin.com/ticket/11808

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25278345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档