我正在尝试在类 LoginForm 中创建一个包含 3 个字段的表单:usuario、senha、msgLogin。我从输入框中收到字段 usuário 和 senha,然后我尝试重定向到具有相同字段和字段 MsgLogin 的同一页面。但我无法更新 msgLogin 字段,我不明白为什么。这些是代码:
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>
控制器:
@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);
}
使用 Lombok 上课:
@Getter
@Setter
public class LoginForm {
private String usuario;
private String senha;
private String msgLogin;
}