Controller SpringMVC does not redirect to another controller

0

No Controller

LoginController.java

@Controller
@RequestMapping("/login")
public class LoginController {

@Autowired
private UsuarioRepositorio usuarioRepositorio;

@RequestMapping(method = RequestMethod.POST)
public String autenticar(@Valid @ModelAttribute Usuario usuario,
        BindingResult bindingResult, Model model, HttpSession session) {

    System.out.println("Logando...");
    Usuario user = new Usuario();
    UsuarioFactory uf = new UsuarioFactory();
    String retorno = "login/paginaLogin";
    String username = "";
    String autenticar = "";
    String msgErro = "";

    if (bindingResult.hasErrors()) {
        throw new LoginInvalidException();
    } else {
        try {
            if (usuario.getUsername() == null) {
                if ((usuario.getEmail() != null)
                        && (!usuario.getEmail().isEmpty())) {
                    username = uf.gerarUsername(usuario.getEmail());
                }
            }

            if ((usuario.getUsername() != null)
                    && (!usuario.getUsername().isEmpty())) {
                username = usuario.getUsername();

                if (username.contains("@")) {
                    username = uf.gerarUsername(username);
                }

            }

            user = usuarioRepositorio.pegarUsuarioPeloUsername(username);

            if (user.getIdUsuario() > 0L) {
                String senha = "";

                senha = usuario.getSenha();

                autenticar = uf.chkAutenticar(user, senha);

                if (autenticar == "") {

                    model.addAttribute("usuarioLogado", usuario);
                    session.setAttribute("usuarioLogado", usuario);
                    retorno = "redirect:minhaConta/" + user.getIdUsuario();

                }

            }
        } catch (Exception e) {
            // TODO: handle exception
            autenticar = "Usuário não encontrado";
        }

    }
    model.addAttribute("usuario", user);
    model.addAttribute("mensagemErro", autenticar);

    return retorno;

}
}

recebe o retorno ="redirect:minhaConta/" + user.getIdUsuario();

Which theoretically should I go to MyContaController.java

@Controller
@RequestMapping("/minhaConta")
public class MinhaContaController {

@Autowired UsuarioRepositorio usuarioRepositorio;

@RequestMapping(method = RequestMethod.GET, value = "{idUsuario}")
public String minhaConta(@PathVariable Long idUsuario, Model model) {

    Usuario usuario = new Usuario();

    usuario = usuarioRepositorio.findOne(idUsuario);

    model.addAttribute("usuario", usuario);

    return "minhaConta/paginaMinhaConta";

}

}

Does not fire any Exception , but does not exit the login screen, and data is being passed normally.

    
asked by anonymous 17.08.2018 / 20:15

2 answers

1

Probably the error is in the if, when you compare authentication with == it compares the reference of the object, not the contents of the string, and can return false:

if (autenticar == "") { ...}

Do not use == to compare strings, use this:

if ("".equals(autenticar)){...}
    
20.08.2018 / 15:36
-2
  • Replace autenticar == "" with autenticar.isEmpty() .

  • Replace redirect:minhaConta/" + user.getIdUsuario(); with redirect:/minhaConta/" + user.getIdUsuario(); .

  • Replace @RequestMapping(method = RequestMethod.GET, value = "{idUsuario}") with @RequestMapping(method = RequestMethod.GET, value = "/{idUsuario}") .

  • 24.08.2018 / 19:25