How to direct call xhml page from ManagedBean based on action method return string without faces-config.xml file

51 Views Asked by At

1. ManagedBean class

@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";
    }
}

2. xhtml files

WebContent\app\common\login.xhtml;
WebContent\app\common\error.xhtml;

WebContent\app\admin\adminHome.xhtml;
WebContent\app\hr\hrHome.xhtml;
....

3. faces-config.xml

<!-- 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>

As you see in the above snap code..

The login.xhtml is common for all users, based on the logined user validation the result page would be decides. if user belongs to HR then app/hr/hrHome.xhtml would be redirect if user belongs to Admin then app/admin/adminHome.xhtml

Requirement : I want to redirect the specific result xhtml page based on the return string of action method login() of LoginBean class instead of using the xml configuration file faces-config.xml.

How can I do it using Primefaces with JSF ?

1

There are 1 best solutions below

2
tsotzolas On

You can do something like

 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"
     }