Springmvc controller does not redirect to another controller

Asked

Viewed 260 times

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();

That theoretically should go to Minhacontacontroller.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";

}

}

Doesn’t fire a shot Exception , but does not leave the login screen, and data is being passed normally.

  • 1

    Are you sure the variable retorno is receiving the correct route? Before the return puts a System.out.println(retorno); to see

  • @Igorventurelli the variable retorno you’re getting redirect:minhaConta/8, the "8" is the idUsuario

2 answers

1


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

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

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

if ("".equals(autenticar)){...}
  • 1

    better yet, use the . isEmpty() method to compare whether the String is empty

  • If it uses the . isEmpty() method it can give a Nullpointerexception if the string is null

  • I used and still persists, spring knows it is to redirect but does not do this action

  • @Tiagoferezin tries with "redirect:/myConta" instead of "redirect:myConta/"

-2

  1. Substitute autenticar == "" for autenticar.isEmpty().

  2. Substitute redirect:minhaConta/" + user.getIdUsuario(); for redirect:/minhaConta/" + user.getIdUsuario();.

  3. Substitute @RequestMapping(method = RequestMethod.GET, value = "{idUsuario}") for @RequestMapping(method = RequestMethod.GET, value = "/{idUsuario}").

Browser other questions tagged

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