将值从JSF传递给bean

时间:2017-06-05 00:09:49

标签: jsf managed-bean

我正在使用JSF和PrimeFaces创建一个Java Web项目,我有下一个表单:

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

我的豆子是:

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

}

但是当我单击按钮时,我的类属性中的数据为空,我尝试调试项目,它似乎创建了一个操作对象,一个用于设置de user,一个用于设置密码,一个用于设置按钮。

有人知道会发生什么吗?

0 个答案:

没有答案