前后台数据交互:
<form class="ui large form" method="post" action="#" th:action="@{/admin/login}">
<div class="ui stacked segment">
<div class="field">
<div class="ui left icon input">
<i class="user icon"></i>
<input type="text" name="username" placeholder="用户名" >
</div>
</div>
<div class="field">
<div class="ui left icon input">
<i class="lock icon"></i>
<input type="password" name="password" placeholder="密码">
</div>
</div>
<button class="ui fluid large teal submit button">登 陆</button>
</div>
<div class="ui error message"></div>
</form>
将input里的name作为参数传递,后台@PostMapping里的参数接收
@PostMapping("/login")
public String login(@RequestParam String username,
@RequestParam String password,
HttpSession session,
RedirectAttributes attributes) {
User user = userService.checkUser(username, password);
System.out.println("---------");
System.out.println(user);
if (user != null) {
user.setPassword(null);
session.setAttribute("user",user);
return "admin/index";
} else {
attributes.addFlashAttribute("message", "用户名和密码错误");
System.out.println("用户名或密码错误:用户名"+username+"密码:"+password);
return "redirect:/admin";
}
}