无法在 PostConstruct 上设置 cookie

Col*_*nta 4 cookies jsf postconstruct

我有这段代码可以在页面加载期间设置 cookie,但是它不起作用:

标记:

<ui:fragment rendered="#{surveyWebBean.showQuestions}">
    <ui:include src="/general/survey.xhtml" />
</ui:fragment>
Run Code Online (Sandbox Code Playgroud)

代码:

调查WebBean.java

@ManagedBean(name = "surveyWebBean")
@SessionScoped
public class EncuestasWebBean extends BaseBean {

    private boolean showQuestions;

    @PostConstruct
    public void init() {
        showQuestions = true;
        UUID uuid = UUID.randomUUID();
        CookieHelper ch = new CookieHelper();
        ch.setCookie("COOKIE_UUID_SURVEY", uuid.toString(), 60 * 60 * 24 * 365 * 10);
    }

    //Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)

CookieHelper.java

public class CookieHelper {

    public void setCookie(String name, String value, int expiry) {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        Cookie cookie = null;
        Cookie[] userCookies = request.getCookies();

        if (userCookies != null && userCookies.length > 0) {
            for (int i = 0; i < userCookies.length; i++) {
                if (userCookies[i].getName().equals(name)) {
                    cookie = userCookies[i];
                    break;
                }
            }
        }

        if (cookie != null) {
            cookie.setValue(value);
        } else {
            cookie = new Cookie(name, value);
            cookie.setPath("/");
        }

        cookie.setMaxAge(expiry);
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        response.addCookie(cookie);
    }

    public Cookie getCookie(String name) {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        Cookie cookie = null;
        Cookie[] userCookies = request.getCookies();

        if (userCookies != null && userCookies.length > 0) {
            for (int i = 0; i < userCookies.length; i++) {
                if (userCookies[i].getName().equals(name)) {
                    cookie = userCookies[i];
                    return cookie;
                }
            }
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试检索 cookie 或在 Google Chrome 检查器或本地数据查看器中检查它时,它不会退出

任何的想法?

Bal*_*usC 5

似乎在呈现响应时首次引用了 bean。您不能在渲染响应阶段设置 cookie。Cookie 设置在 HTTP 响应标头中。但是目前 JSF 正忙于为响应正文生成一些 HTML 输出,响应标头可能已经发送了很长时间。

您需要将第一位写入响应正文之前设置 cookie 。

您可以<f:event type="preRenderView">在渲染响应实际开始之前使用它来调用 bean 侦听器方法。

<f:event type="preRenderView" listener="#{surveyWebBean.init}" />
Run Code Online (Sandbox Code Playgroud)
<f:event type="preRenderView" listener="#{surveyWebBean.init}" />
Run Code Online (Sandbox Code Playgroud)

具体问题无关,至于CookieHelper,也许您在基于 JSF 1.x 的资源中找到了它们,但是您需要知道,自 JSF 2.0 以来,有新的 cookie 相关方法,ExternalContext例如getRequestCookieMap()应该简化获取 cookie 的方法。FacesJSF 实用程序库OmniFaces的实用程序类也有一些与 cookie 相关的方法。

Faces.addResponseCookie("cookieName", cookieValue, "/", 60 * 60 * 24 * 365 * 10);
Run Code Online (Sandbox Code Playgroud)
public void init() { // (remove @PostConstruct!)
    if (showQuestions) {
        return; // Already initialized during a previous request in same session.
    }

    showQuestions = true;
    UUID uuid = UUID.randomUUID();
    CookieHelper ch = new CookieHelper();
    ch.setCookie("COOKIE_UUID_SURVEY", uuid.toString(), 60 * 60 * 24 * 365 * 10);
}
Run Code Online (Sandbox Code Playgroud)