JSF 2 redirection issue

I have the following two methods in my bean support -

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 do not know why the redirection works in the first method (i.e. validateUser ()), but it does not work in the second method (i.e. logout ()). The code inside the logout method actually executes, the session also becomes invalid, but somehow the browser remains on one page. And I use PrimeFaces p: commandButton, and ajax is enabled for both of them. Any, any idea? Thank.

+3
source share
1 answer

but somehow the browser remains on one page. And I use PrimeFaces p: commandButton, and ajax is enabled for both of them

, . , - . ajax="false" <p:commandButton>.


, javax.servlet JSF beans. , - . JSF2 :

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

/ .

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

bean ().

. :

+5

All Articles