jakarta-eemanaged-beanwildfly-9

Managed Bean implicit routing not working?


I am having trouble navigating implicitly from managed bean, the page is a login page containing username and password fields, used to check with phpmyadmin database;

My managed bean is defined like so:

package backManagedBean;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

import com.esprit.entities.Users;
import com.esprit.service.AuthenticationService;


@SuppressWarnings({ "serial", "unused" })
@RequestScoped
@ManagedBean(name="loginManagedBean", eager=true )
public class loginManagedBean implements Serializable {

    /**
     * 
     */

    private static Users connectedUser;
    private String emailField;
    private String passwordField;
    @EJB
    AuthenticationService authService;


    public String getEmailField() {
        return emailField;
    }
    public void setEmailField(String emailField) {
        this.emailField = emailField;
    }
    public String getPassworField() {
        return passwordField;
    }
    public void setPassworField(String passwordField) {
        this.passwordField = passwordField;
    }
    public AuthenticationService getAuthService() {
        return authService;
    }
    public void setAuthService(AuthenticationService authService) {
        this.authService = authService;
    }

    public String login(){
        //connectedUser = authService.authenticate(emailField, passwordField);
        return "/Back/index?faces-redirect=true";
    }



}

The page refresh and the query execute normaly and you can see wildfly server log as defined in persistance for hibernate.show_sql. Yet is goes back the login page as nothing is returned from the login() function.

login.xhtml :

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
  <ui:composition template="../template/facesTemplate.xhtml">
    <ui:define name="head">
        <title>Welcome To Petroca</title>

    </ui:define>


    <ui:define name="body">
    <div class="accountbg">
        <div class="content-center">
            <div class="content-desc-center">
                <div class="container">
                    <div class="row justify-content-center">
                        <div class="col-lg-5 col-md-8">
                            <div class="card">
                                <div class="card-body">
                                    <h3 class="text-center mt-0 m-b-15"><a href="index.html"
                                            class="logo logo-admin"><img src="../assets/images/logo-dark.png" height="30"
                                                alt="logo" /></a></h3>
                                    <h4 class="text-muted text-center font-18"><b>Sign In</b></h4>
                                    <div class="p-2">
                                        <h:form class="form-horizontal m-t-20" >
                                            <div class="form-group row">
                                                <div class="col-12"><h:inputText class="form-control" type="text" required="" value="#{loginManagedBean.emailField}" placeholder="Username" /></div>
                                            </div>
                                            <div class="form-group row">
                                                <div class="col-12">
                                                <h:inputSecret class="form-control" required="" value="#{loginManagedBean.passworField}" placeholder="Password" /></div>
                                            </div>
                                            <div class="form-group text-center row m-t-20">
                                                <div class="col-12">
                                                    <h:button class="btn btn-primary btn-block waves-effect waves-light" action="#{loginManagedBean.login()}">
                                                        Log In
                                                    </h:button>
                                                </div>
                                            </div>
                                            <div class="form-group m-t-10 mb-0 row">
                                                <div class="col-sm-7 m-t-20"><a href="passwordRecovery.jsf"
                                                        class="text-muted"><i class="mdi mdi-lock"></i> Forgot your
                                                        password?</a></div>
                                            </div>
                                        </h:form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div><!-- end row -->
                </div>
            </div>
        </div>
    </div>
    </ui:define>

  </ui:composition> 
</html>

Solution

  • I would probably use this:

    <h:commandButton ... action="#{loginManagedBean.login}" />
    

    What your button actually should do, is form submission with some data (login/pwd) and in case it is ok - redirect to some other page, or if auth fails - to some error page let's say. So commandButton is a better choice for this purpose.

    The <h:commandButton> generates a HTML like <input type="submit"> button which submits by default the parent <h:form> using POST method and invokes the actions attached to action or actionListener.

    You could also try to use outcome property to see if it makes any difference:

    <h:button ... outcome="#{loginManagedBean.login()}" />
    

    But in your case commandButton is a better option.