Avi*_*rua 2 html spring thymeleaf spring-boot
Now i am doing project with Spring Boot. I wanted to show error message when I fail to login. When I did Maven project I did it easily with BindingResult. I followed several instruction to complete the task what I wanted to do. But I couldn't get any proper solution. I need a proper solution in simplest way.
Here my 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";
}
Run Code Online (Sandbox Code Playgroud)
And my html file is
<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>
Run Code Online (Sandbox Code Playgroud)
您可以在 URL 中添加一个错误参数,例如 localhost:8080/login?error。然后,在 Thymeleaf 的帮助下,您可以将这个 div 添加到 html 中。
<div th:if="${param.error}" class="alert alert-danger">
Invalid username and password.
</div>
Run Code Online (Sandbox Code Playgroud)
如果 URL 中存在错误参数,则会显示此 div。
但是,您可能需要更改
return "Login"
Run Code Online (Sandbox Code Playgroud)
到
return "redirect:Login?error";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5529 次 |
| 最近记录: |