Vaadin 7:如何更新Cookie值?

时间:2014-08-13 04:55:36

标签: cookies vaadin vaadin7

我使用的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选择,创建和销毁,但我无法使用方法updateCookieValue(final String name, final String value)更新Cookie的值。我在Firefox和Chrome浏览器中都测试为updateCookieValue(LOCALE_COOKIE, "en");,但浏览器的区域设置值没有变化。我的方法有什么问题?

1 个答案:

答案 0 :(得分:2)

如果您在推送时使用Vaadin,则只能在执行UI.init()方法时更新Cookie。目前没有针对此问题的解决方法。

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