2012-05-20 32 views
1

我有绑定到支持托管bean的用户名和密码。 在backing bean中,当我使用数据库检查用户名和密码时,我想将页面从login.xhtml重定向到home.xhtml。我怎样才能做到这一点?如何登录后重定向?

+0

您使用的是jsf 2还是jsf 1.x? – Daniel

+0

@Daniel:基于他的问题历史,JSF 2.0。 – BalusC

回答

6

只返回带有faces-redirect=true参数的视图ID。

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). 
    } 
} 
+0

然后呢? 我的意思是,我在哪里使用返回的字符串? 我有一个login.html页面,上面写着login.java bean。用这种方法,我该如何将用户重定向到一个全新的页面? –

+0

你有什么试过?它怎么不适合你?你不需要做其他事情。如果视图ID正确,则应显示新页面。上面的答案假定它是'home.xhtml',它与'login.xhtml'在同一个文件夹中。 – BalusC

+0

好吧,明白了。现在你能告诉我这是如何工作的吗? faces-redirect是一个保留参数,对吧?并告诉我它的工作。 –