1.效果
登陆成功则跳转到新的页面,失败则返回登陆页面,并且进行提示
2.login.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" th:href="@{/img/favicon.ico}">
<title>Signin Template for Bootstrap</title>
<!--引入webjars中的bootstrap的css -->
<link th:href="@{/webjars/bootstrap/4.3.1/css/bootstrap.css}" rel="stylesheet">
<!--引用bootstrap官方登陆页面的css -->
<link th:href="@{/css/signin.css}" rel="stylesheet">
</head>
<body>
<div class="container">
<form class="form-signin" th:action="@{/login}" method="post">
<h2 class="form-signin-heading" th:text="#{login.tip}" >Please sign in</h2>
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
<label for="inputEmail" class="sr-only" th:text="#{login.email}">Email address</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus th:placeholder="#{login.email}" name="inputEmail">
<label for="inputPassword" class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required th:placeholder="#{login.password}" name="inputPassword">
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me">[[#{login.remember}]]
</label>
</div>
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.submit}">Sign in</button>
</form>
</div> <!-- /container -->
</body>
</html>
错误提示的关键代码:<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
3.Controller
@PostMapping("login")
public String login(@RequestParam("inputEmail") String email, @RequestParam("inputPassword") String password, Map<String,Object> map){
if(email.equals("xxxxxx") && password.equals("xxxx")){
return "dashboard";
}else{
map.put("msg","账号密码错误!");
return "index";
}
}