Search code examples
jsfhttp-redirectmanaged-bean

How to redirect after login?


I've username and password bound to the backing managed bean. In the backing bean, when I check the username and password with DB, I want to redirect the page from login.xhtml to home.xhtml. How can I do that?


Solution

  • Just return the view ID appended with faces-redirect=true parameter.

    E.g.

    public String login() {
        User found = userService.find(username, password);
    
        if (found != null) {
            this.user = found;
            return "home?faces-redirect=true"; // Will redirect to home.xhtml.
        }
        else {
            addGlobalErrorMessage("Unknown login, please try again");
            return null; // Will stay in current view (and show error message).
        }
    }