@Named
public class LoginBean{
private String username;
private String password;
private String userType;
// setter getter
public String login() {
...
if(isUserValidate()){
if(userType.equals("HR"){
return "HRHOME";
}
if(userType.equals("ADMIN"){
return "ADMINHOME";
}
if(userType.equals("MANAGER"){
return "MANAGERHOME";
}
}
return "FAILURE";
}
}
WebContent\app\common\login.xhtml;
WebContent\app\common\error.xhtml;
WebContent\app\admin\adminHome.xhtml;
WebContent\app\hr\hrHome.xhtml;
....
<!-- Case 0 for login user -->
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>LOGIN</from-outcome>
<to-view-id>/app/common/login.xhtml</to-view-id>
</navigation-case>
<!-- Case 1 if login user is from HR -->
<from-view-id>/app/common/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>HRHOME</from-outcome>
<to-view-id>/app/hr/hrHome.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>ADMINHOME</from-outcome>
<to-view-id>/app/admin/adminHome.xhtml</to-view-id>
</navigation-case>
...
<navigation-case>
<from-outcome>FAILURE</from-outcome>
<to-view-id>/app/common/error.xhtml</to-view-id>
</navigation-case>
如您在上面的快照代码中看到的。
login.xhtml对所有用户都是通用的,根据登录的用户验证,将确定结果页面。如果用户属于HR,则app / hr / hrHome.xhtml将被重定向如果用户属于管理员,则app / admin / adminHome.xhtml
要求: 我想基于LoginBean类的操作方法login()的返回字符串重定向特定的结果xhtml页面,而不是使用xml配置文件faces-config.xml。
我如何将Primefaces与JSF一起使用?
您可以做类似的事情
if (isUserValidate()) {
return "/WebContent\app\hr\hrHome.jsf?faces-redirect=true";
} else if(userType.equals("ADMIN"){ {
return "/WebContent\app\admin\adminHome.jsf?faces-redirect=true";
} else if(.......){
.........
}else{
return "/WebContent\app\admin\error.jsf?faces-redirect=true"
}