html - 当用户使用 Spring Boot 登录失败时,如何在登录页面中显示错误消息?
问题描述
现在我正在用 Spring Boot 做项目。我想在登录失败时显示错误消息。当我做 Maven 项目时,我很容易用 BindingResult 做到了。我按照几个指示完成了我想做的任务。但我无法得到任何适当的解决方案。我需要以最简单的方式提供适当的解决方案。
这是我的Controller
课
@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";
推荐阅读
- javascript - 我可以使用 Infinity 和 -Infinity 作为 Max 和 Min 变量的初始值吗?
- flutter - 如何在 google admob 上删除测试广告文字
- amazon-s3 - 如何在不使用 Confluent 的情况下使用 Kakfa Connect 从 Kafka 将数据发送到 AWS S3?
- javascript - javascript使用中的正则表达式失败
- html - 修复图像附近名称标签的问题
- r - 由于 R 中的数据类型问题,无法过滤数据帧
- android - Android Xamarin 应用程序 - 如何在设备屏幕关闭然后重新打开时保持表单状态?
- python-3.x - 根据字典列表和特定日期条件替换列值 - 使用 if 和 for 循环 - Pandas
- flutter - 重新安排 Flutter 时,本地通知不会在新时间触发
- javascript - 如何使用云功能从我的 Firebase 实时数据库中的 database.ref 正在侦听的节点内部获取数据?