0

I am trying to create a form with 3 fields within the class LoginForm: usuario, senha, msgLogin. I receive the fields usuário and senha from the input boxes and then I try to redirect to the same page with the same fields plus the field MsgLogin. But I've not been able to update the msgLogin field and I don't understand why. These are the code:

HTML:

<form id="frmLogin" class="form col-md-12" action="#" th:action="@{/login}" th:object="${loginForm}" method="post">
        <div class="form-group">
            <label for="usuario" class="col-md-1">Usuário: </label>
            <input type="text" id="usuario" placeholder="Email" th:field="*{usuario}"/>
        </div>

        <div class="form-group">
            <label for="senha" class="col-md-1">Senha: </label>
            <input type="password" id="senha" placeholder="senha" th:field="*{senha}"/>
        </div>

        <div class="row">
            <button id="entrar">Entrar</button>
        </div>

    <div class="row">
        <div id="msgLogin"></div>
        <p th:text="${loginForm.msgLogin}" />
    </div>
</form>

The Controller:

@RequestMapping("/")
public String init(@ModelAttribute LoginForm loginForm) {
    Logger.getAnonymousLogger().info("Tela Inicial.");
    return "home";
}

@PostMapping("/login")
public ModelAndView entrar(LoginForm loginForm) throws IOException {
    Logger.getAnonymousLogger().info("Entrando no Internet Banking.");

    service.login(usuario, senha);

    ModelMap model = new ModelMap();
    loginForm.setMsgLogin("I want to update that value!");
    model.addAttribute("loginForm", loginForm);
    return new ModelAndView("redirect:/", model);
}

Class using Lombok:

@Getter
@Setter
public class LoginForm {

    private String usuario;

    private String senha;

    private String msgLogin;

}
  • Do you want to add the value of `loginForm.msgLogin` in the same form along with `usuario` and `senha` ? or other else? – Zico Jul 06 '17 at 05:51
  • Actually I want both things. I want to use a if condition and in one case I would like to send msgLogin field to the same form and in the other case I would like to send usuário and senha to another form. – user3450923 Jul 06 '17 at 13:23

2 Answers2

0

A redirect send a 302 HTTP status back to the browser to resubmit using a new url, so it resubmits the same two fields to the new url. There's no expectation of payload data in the 302 response.

0

In your Controller, change the last line in your entrar method to: return new ModelAndView("/login", model);

Luay Abdulraheem
  • 751
  • 3
  • 11