Problems with if no managedbean

Asked

Viewed 18 times

1

I have a login form with the registration and password fields. I am trying to validate this login where only the number "92018" can log in to the system, but when the login method is called, it does not pass by if, even having typed the correct value, I already put a breakpoint to see if the value would be different, but it is not. What could be?

Managedbean

public String Logar()
    {
            if(associado.getMatricula()=="92018")
            {
                return "/Teste";    
            }

        return"/sucesso";   
    }

Login form

<h:form id="formLogin">
            <p:fieldset legend="Insira os dados de acesso"
                style="margin-bottom:20px">
                <p:outputLabel for="matricula" value="Matricula: " />
                <br />
                <p:inputText id="matricula" value="#{LoginBean.associado.matricula}"
                    required="true" autocomplete="off" />
                <br />
                <p:outputLabel for="senha" value="Senha: " />
                <br />
                <p:inputText id="senha" value="#{LoginBean.associado.senha}"
                    required="true" />
                <br />
                <br />
                <p:commandButton value="Entrar" id="logar"
                    action="#{LoginBean.Logar}" ajax="false" />
                <br />
            </p:fieldset>
        </h:form>

Class

public class Associado {

    private String matricula,senha;

    public String getMatricula() {
        return matricula;
    }

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }

    public String getSenha() {
        return senha;
    }

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

}

1 answer

3


Do not compare strings to == in Java, this will compare the address of the objects containing your words, rather than comparing the words themselves. To compare strings use the equals method, this way:

String palavra = "StackOverflow";

if (palavra.equals("StackOverflow")) {
    //são iguais
}
  • It worked! Thanks! I started studying java yesterday and I didn’t know this detail.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.