登录后如何重定向?

Mud*_*zad 1 jsf redirect managed-bean

我将用户名和密码绑定到支持托管bean.在支持bean中,当我用DB检查用户名和密码时,我想将页面重定向login.xhtmlhome.xhtml.我怎样才能做到这一点?

Bal*_*usC 6

只需返回附加faces-redirect=true参数的视图ID即可.

例如

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).
    }
}
Run Code Online (Sandbox Code Playgroud)