JSF 2 重定向问题

发布于 2024-11-08 20:12:49 字数 1505 浏览 0 评论 0原文

我的支持 bean 中有以下两个方法 -

public String validateUser() {
    FacesContext facesCtx = FacesContext.getCurrentInstance();

    if(userName.equals("user1") && password.equals("pass1")) {
        User user = new User();
        user.setUserName(userName);
        HttpSession session = (HttpSession) facesCtx.getExternalContext().getSession(false);
        session.setAttribute(User.SESSION_ATTRIBUTE, user);
        return "secured/home.jsf?faces-redirect=true";
    }

    if(!userName.equals(LoginBean.USERNAME)) {
        FacesMessage msgForUserName = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Username did not match.", null);
        facesCtx.addMessage("loginForm:userName", msgForUserName);
    }

    if(!password.equals(LoginBean.PASSWORD)) {
        FacesMessage msgForPassword = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password did not match.", null);
        facesCtx.addMessage("loginForm:password", msgForPassword);
    }

    return null;
}

public String logout() {
    logger.info("Logging out .........................................");
    FacesContext facesCtx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) facesCtx.getExternalContext().getSession(false);
    session.invalidate();
    return "login.jsf?faces-redirect=true";
}

我不知道为什么重定向在第一个方法(即 validateUser())中起作用,但在第二个方法(即 logout())中不起作用。 注销方法中的代码实际上被执行了,会话也变得无效,但不知何故浏览器仍停留在同一页面上。 而且,我正在使用 PrimeFaces p:commandButton,并且它们都启用了 ajax。 任何人,任何想法吗? 谢谢。

I have following two methods in my backing bean -

public String validateUser() {
    FacesContext facesCtx = FacesContext.getCurrentInstance();

    if(userName.equals("user1") && password.equals("pass1")) {
        User user = new User();
        user.setUserName(userName);
        HttpSession session = (HttpSession) facesCtx.getExternalContext().getSession(false);
        session.setAttribute(User.SESSION_ATTRIBUTE, user);
        return "secured/home.jsf?faces-redirect=true";
    }

    if(!userName.equals(LoginBean.USERNAME)) {
        FacesMessage msgForUserName = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Username did not match.", null);
        facesCtx.addMessage("loginForm:userName", msgForUserName);
    }

    if(!password.equals(LoginBean.PASSWORD)) {
        FacesMessage msgForPassword = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password did not match.", null);
        facesCtx.addMessage("loginForm:password", msgForPassword);
    }

    return null;
}

public String logout() {
    logger.info("Logging out .........................................");
    FacesContext facesCtx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) facesCtx.getExternalContext().getSession(false);
    session.invalidate();
    return "login.jsf?faces-redirect=true";
}

I don't know why the redirection is working in the first method (i.e. validateUser()), but it's not working in the second method (i.e. logout()).
The code inside the logout method is actually executed, the session also gets invalidated,but somehow the browser stays on the same page.
And, I am using PrimeFaces p:commandButton and the ajax is enabled on both of them.
Any one, any idea?
Thank you.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2024-11-15 20:12:49

但不知何故,浏览器停留在同一页面上。而且,我正在使用 PrimeFaces p:commandButton 并且它们都启用了 ajax

我不认为它会失败。我怀疑这与无效的会话有关。在 上使用 ajax="false" 进行尝试。


与该问题无关,您应该尽量减少 JSF 托管 bean 中的 javax.servlet 导入。它们通常表明您在错误的地方或以笨拙的方式做事。在纯 JSF2 中,您可以按如下方式使会话失效:

FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

您可以通过会话映射获取/设置会话中的对象。

Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
// ...

或者只是将其设为托管 bean(属性)。

另请参阅:

but somehow the browser stays on the same page. And, I am using PrimeFaces p:commandButton and the ajax is enabled on both of them

I wouldn't expect it to fail. I suspect that this has something to do with the invalidated session. Try it with ajax="false" on the <p:commandButton>.


Unrelated to the problem, you should try to minimize the javax.servlet imports in your JSF managed beans. They often indicate that you're doing things in the wrong place or the clumsy way. In pure JSF2, you can invalidate the session as follows:

FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

You can get/set objects in the session by the session Map.

Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
// ...

Or just make it a managed bean (property).

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文