从表单到管理员的空值

时间:2013-06-30 17:26:55

标签: java jsf web-applications

我正在做我的第一个JSF应用程序,我正在实现登录页面。但是当我用登录名和密码提交表单时,我遇到了问题。我使用JSF 2.0

我有以下表格:

<h:form>
            <h:panelGrid columns="2">
                <p:outputLabel for="txt_login" value="Login" />
                <p:inputText id="txt_login" value="#{loginBean.login}"/>
                <p:outputLabel for="txt_senha" value="Senha" />
                <p:inputText id="txt_senha" value="#{loginBean.senha}"/>
            </h:panelGrid>
            <h:panelGrid columns="2">
                <p:commandButton value="Login" action="#{loginBean.efetuaLogin}" />
                <p:commandButton value="Cancelar" action="cancelar" />
            </h:panelGrid>
</h:form>

ManagedBean:

@ManagedBean
public class LoginBean {

private String login;
private String senha;

public String getSenha() {
    return senha;
}

public void setSenha(String senha) {
    System.out.println("SENHA = " + this.getSenha());
    this.senha = senha;
}

public String getLogin() {
    return login;
}

public void setLogin(String login) {
    System.out.println("USER = " + this.getLogin());
    this.login = login;
}

public String efetuaLogin() {
    Usuario user = new Usuario(this.login, this.senha);
    System.out.println("USER = " + this.getLogin() + " SENHA = " + this.getSenha());
    UsuarioController.getInstance().save(user);
    return "sucesso";

}

public String cadastar() {
    Usuario user = new Usuario(this.login, this.senha);
    System.out.println("USER = " + this.login + " SENHA = " + this.senha);
    UsuarioController.getInstance().save(user);
    return "sucesso";
}

}

提交这些表单时,我的托管bean传递null。为什么会这样?

2 个答案:

答案 0 :(得分:0)

为ManagedBean添加范围,对于登录添加@RequestScoped。您使用<h:inputText作为密码。请使用<h:inputSecret/>获取密码。

答案 1 :(得分:0)

您过早地致电System.out.println,因此您认为自己正在null,但事实并非如此。示例:

public void setSenha(String senha) {
    System.out.println("SENHA = " + this.getSenha());
    this.senha = senha;
}

应该是

public void setSenha(String senha) {
    this.senha = senha;
    System.out.println("SENHA = " + this.getSenha());
}

setLogin(String login)

相同

更新#2 你的问题必须在其他地方。检查您是否使用了正确的名称空间命名空间

xmlns:p="http://primefaces.org/ui

据我所知,所有的primefaces组件都适合我。这里的例子是我的代码。尝试针对您发布的原始.xhtml运行它。唯一的区别是我上面告诉你的编辑而我的LoginBean没有UsuarioController.getInstance().save(user);

import javax.faces.bean.ManagedBean;

@ManagedBean
public class LoginBean {

    private String login;
    private String senha;

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
        System.out.println("SENHA = " + this.getSenha());

    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
        System.out.println("USER = " + this.getLogin());
    }

    public String efetuaLogin() {
        User user = new User(this.login, this.senha);
        System.out.println("USER = " + this.getLogin() + " SENHA = " + this.getSenha());
        System.out.println("ACTUAL USER = " + user.getLogin() + " ACTUAL SENHA = " + user.getSenha());
        return "sucesso";
    }

    public String cadastar() {
        return "sucesso";
    }
}


public class User {
    private String login; 
    private String senha; 

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public User(String login, String senha) {
        this.login = login;
        this.senha = senha;
    }

    public User() {
    }
}