Spring returning NULL in only one of the form fields

Asked

Viewed 200 times

0

I am learning Java and am having a problem with a form using Spring. I even found a similar problem here on the forum but a little different from mine.

It is a basic login page, containing only two fields, login and password. The problem is that when I submit the form, the password is filled in correctly but the login always comes with NULL.

Something’s missing or I’m making a mistake in the code?

Any comments and/or criticisms to this code are welcome, after all I am here to learn, remembering that it is just a simple code to assimilate knowledge.

Thank you in advance to anyone who can help. Thank you very much!

This is my page:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
    <body>
        <c:import url="/WEB-INF/jsp/cabecalho.jsp" />

        <h3>Efetue seu Login</h3>

        <form action="efetuaLogin" method="post">           
            Login: <input type="text" name="login" /><br/>          
            Senha: <input type="password" name="senha" /><br/>          
            <input type="submit" value="Entrar" />
        </form> 

        <br/>
        <a href="http://localhost:8180/fj21-tarefas">Home</a>
        <a href="http://localhost:8180/fj21-tarefas/listaTarefas">Listar Tarefas</a>        

        <br/>
        <c:import url="/WEB-INF/jsp/rodape.jsp" />
    </body>
</html>

Controller:

package br.com.stefanini.tarefas.controller;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import br.com.stefanini.tarefas.servlet.Usuario;
import br.com.stefanini.tarefas.servlet.UsuarioDao;

@Controller
public class LoginController {

    @RequestMapping("loginForm")
    public String loginForm(){
        System.out.println("Aqui");
        return "login";
    }

    @RequestMapping("efetuaLogin")
    public String efetuaLogin(Usuario usuario, HttpSession session){

        if (new UsuarioDao().existeUsuario(usuario)) {
            session.setAttribute("usuariologado", usuario);
            return "menu";
        }
        return "redirect:loginForm";
    }
}

Model:

package br.com.stefanini.tarefas.servlet;

public class Usuario {

    private String login;    
    private String senha;    

    public Usuario() {
        super();
    }

    //Getters
    public String getLogin() {
        return login;
    }

    public String getSenha() {
        return senha;
    }

    //Setters
    public void setUsuario(String login) {
        this.login = login;
    }

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

1 answer

0

In his Model Usuario, change the method setUsuario for setLogin

Browser other questions tagged

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