gpt4 book ai didi

html - 当用户无法使用 Spring Boot 登录时,如何在登录页面中显示错误消息?

转载 作者:行者123 更新时间:2023-12-04 14:37:58 24 4
gpt4 key购买 nike

现在我正在用 Spring Boot 做项目。我想在登录失败时显示错误消息。当我做 Maven 项目时,我用 BindingResult 很容易地做到了。我按照几条指示完成了我想做的任务。但我无法得到任何适当的解决方案。我需要以最简单的方式找到合适的解决方案。

这是我的 Controller类(class)

  @RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
public String checkLogin(@RequestParam String roll, @RequestParam String pass) {
if (studentService.existsByRollAndPass(roll, pass)) {
return "Welcome";
} else {
return "Login";
}

还有我的 html文件是
  <form class="form-horizontal" method="post" action="/loginCheck">

<fieldset>


<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Roll</label>
<div class="col-md-4">
<input id="textInput" name="roll" type="text"
placeholder="Roll" class="form-control input-md">

</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="password">Password</label>
<div class="col-md-4">
<input id="password" name="pass" type="password"
placeholder="password" class="form-control input-md">

</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="login"></label>
<div class="col-md-4">
<button id="login" name="login" class="btn btn-primary">Login</button>
</div>
</div>

</fieldset>

</form>

最佳答案

您可以在 URL 中添加一个错误参数,例如 localhost:8080/login?error。然后,在 Thymeleaf 的帮助下,您可以将这个 div 添加到 html 中。

<div th:if="${param.error}" class="alert alert-danger">    
Invalid username and password.
</div>

如果 URL 中存在错误参数,则会显示此 div。

但是,您可能需要更改
return "Login"


return "redirect:Login?error";

关于html - 当用户无法使用 Spring Boot 登录时,如何在登录页面中显示错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50432387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com