Vaadin 7 : How to update Cookie value?

偶尔善良 提交于 2019-12-11 14:37:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!