1

I'm making a Java Web project with JSF and PrimeFaces, i have the next form:

<h:form>
    <p:inputText value="#{LoginBean.username}" />
    <p:inputText value="#{LoginBean.password}" />
    <p:commandButton value="Iniciar Sesión" actionListener="#{LoginBean.login}" ajax="false" ></p:commandButton>
</h:form>

And my bean is:

public class LoginBean {

/**
 * Creates a new instance of LoginBean
 */
public LoginBean() {

}

private String username;
private String password;

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String login() {
    System.out.println("hola");
    EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Persistence" );
    EntityManager entitymanager = emfactory.createEntityManager();
    Users user = new Users();
    Query query = entitymanager.createNamedQuery("Users.findLogin", Users.class);
    System.out.print(query.getParameter("email"));
    query.setParameter("password", this.password);
    query.setParameter("email", this.username);
    Collection<Users> results = query.getResultList();
    System.out.println(username);
    System.out.println(password);
    for(Users x : results)
    {
        System.out.println(x.getEmail());
    }
    if (results.size() < 1) {
        username = null;
        password = null;
        System.out.println("Fue nulo");
        return null;
    } else {
        System.out.println("No fue nulo");
        return "userhome?faces-redirect=true";
    }
}

public String logout() {   
    return "index?faces-redirect=true";
}

}

But when I click the button the data in my class properties are nulls, I try debugging the project and it appears that it creates an object for action, one for setting de user, one for the password and one for the button.

Does someone knows what happen?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452

0 Answers0