与JSF无效后,保持当前的语言环境

发布于 2025-01-18 08:12:18 字数 678 浏览 0 评论 0原文

此方法使会话无效并使用户返回主页。

public String restart() {
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return "/index?faces-redirect=true";
}

因此,用户选择的区域设置会被忘记,应用程序会切换回浏览器的默认区域设置。 我希望即使在会话失效后区域设置也保持不变。该方法现在看起来像:

public String restart() {
    Locale currLocale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    FacesContext.getCurrentInstance().getViewRoot().setLocale(currLocale);

    return "/index?faces-redirect=true";
}

但区域设置仍然切换回浏览器的默认值。我怎样才能实现我的目标?

This method invalidates the session and gets the user back to the home page.

public String restart() {
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return "/index?faces-redirect=true";
}

As a consequence, the locale chosen by the user is forgotten and the app switches back to the default locale of the browser. I want that the locale remains the same even after the session invalidation. The method now looks like:

public String restart() {
    Locale currLocale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    FacesContext.getCurrentInstance().getViewRoot().setLocale(currLocale);

    return "/index?faces-redirect=true";
}

But the locale still switches back to the browser's default. How can I achieve my goal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文