0

I am tryint to do the redirection with the aid of the navigation-rule in the faces-config.xml file but I am facing problem I am not being redirected to the welcome.xhtml page from the index.xhtml page after clicking the login button. The login() as well as validateUser() are being invoked in the LoginBeanCDI bean and I am getting seccuss as a result back but the rediection does not happen.

I test the funktioniliy of the pages with commented goToWelcome() in the LoginBeanCDI bean after call it from the form action <h:commandButton value="Login" id="combtn"action="#{loginBeanCDI.goToWelcome()}"/> and in this case I am being redirected to the welcome.xhtml page without any problem. Therefore I think the problem is with the navigation rule in the faces-config.xml file.

index.xhtml

 <!DOCTYPE html>
<html xmlns="htpp://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core">


<ui:composition template="/WEB-INF/templates/template.xhtml">
    <ui:define name="css">
        <h:outputStylesheet library="css" name="mainStlye.css" />
    </ui:define>
    <ui:define name="content">
        <h:form id="login-form" prependId="false">
            <h:panelGrid columns="5">
                <h:outputLabel value="Username" for="username" />
                <h:inputText id="username" value="#{loginBeanCDI.user.username}" />
                <h:outputLabel value="Password" for="pwd" />
                <h:inputSecret id="pwd" value="#{loginBeanCDI.user.password}" />
                <h:commandButton value="Login" id="combtn"
                    action="#{loginBeanCDI.login()}" />
            </h:panelGrid>
        </h:form>
        <br />

    </ui:define>
</ui:composition>
</html>

strong text

<!DOCTYPE html>
<html xmlns="htpp://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">


<ui:composition template="/WEB-INF/templates/template.xhtml">

 <ui:define name="content"> 
    Welcome back #{loginBeanCDI.user.username} 
 </ui:define>

</ui:composition>
</html>

LoginBeanCDI

package com.beans;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.inject.Named;

import com.entities.User;


@Named(value = "loginBeanCDI")
@SessionScoped
public class LoginBeanCDI implements Serializable {
    private User user;

    public LoginBeanCDI() {

    }

    @PostConstruct
    public void initialize() {
        user = new User();

    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

//  public String goToWelcome() {
//      System.out.println("Inside goToWelcome(): " + user.getUsername() + ", " + user.getPassword());
//      return "/pages/welcome?faces-redirect=true";
//  }

    public String login() {
        System.out.println("login() was called");
        if(validateUser()){
            System.out.println("login() was called. success");
            return "success";
        }else{
            System.out.println("login() was called. fail");
            return "fail";
        }       
    }

    private boolean  validateUser(){
        System.out.println("validateUser() was called");
        return true;
    }
}

faces-config.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

    <application>
        <resource-bundle>
            <base-name>com.stack.bundle</base-name>
            <var>bundle</var>
        </resource-bundle>
    </application>

    <navigation-rule>
        <from-view-id>/index.xhtml</from-view-id>

        <navigation-case>
            <from-action>#{loginBeanCBI.login()}</from-action>
            <from-outcome>success</from-outcome>
            <to-view-id>/pages/welcome</to-view-id>
            <redirect />
        </navigation-case>

        <navigation-case>
            <from-action>#{loginBeanCBI.login()}</from-action>
            <from-outcome>fail</from-outcome>
            <to-view-id>/pages/fail</to-view-id>
            <redirect />
        </navigation-case>

    </navigation-rule>

</faces-config>

Project structure enter image description here

Mr Asker
  • 2,300
  • 11
  • 31
  • 56

1 Answers1

1

You have a typo in your navigation case.

Change

 <from-action>#{loginBeanCBI.login()}</from-action>

To

 <from-action>#{loginBeanCDI.login()}</from-action>
Paul Wasilewski
  • 9,762
  • 5
  • 45
  • 49