java springboot开发中写一个登陆页面在登陆成功弹出一个对话框显示登录成功,失败弹出一个问题框显示登录失败并点击确定后返回登录页面。用thymeleaf怎么写代码
时间: 2023-05-19 15:05:28 浏览: 302
您可以使用以下代码实现:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h1>Login Page</h1>
<form th:action="@{/login}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<br>
<button type="submit">Login</button>
</form>
<div th:if="${param.error}">
<p>Login failed. Please try again.</p>
</div>
<div th:if="${param.logout}">
<p>You have been logged out.</p>
</div>
<div th:if="${param.success}">
<script th:inline="javascript">
alert([[${param.success}]]);
</script>
</div>
</body>
</html>
```
在控制器中,您可以使用以下代码来处理登录请求:
```java
@Controller
public class LoginController {
@GetMapping("/login")
public String showLoginForm() {
return "login";
}
@PostMapping("/login")
public String processLogin(@RequestParam String username, @RequestParam String password, Model model) {
if (username.equals("admin") && password.equals("password")) {
model.addAttribute("success", "Login successful!");
} else {
model.addAttribute("error", true);
}
return "login";
}
}
```
当用户登录成功时,将在页面上显示一个对话框,显示登录成功消息。如果登录失败,将弹出一个问题框,显示登录失败消息,并在用户点击确定后返回登录页面。