帮我用springboot写个登陆界面
时间: 2023-07-01 15:06:12 浏览: 55
可以的,下面是一个简单的登录界面的示例代码:
首先,你需要在 pom.xml 中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
然后,创建一个 LoginController 类,用于处理登录请求:
```java
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
@PostMapping("/login")
public String doLogin(@RequestParam String username, @RequestParam String password, HttpSession session) {
if ("admin".equals(username) && "123456".equals(password)) {
session.setAttribute("username", username);
return "redirect:/dashboard";
} else {
return "login";
}
}
@GetMapping("/dashboard")
public String dashboard(HttpSession session) {
if (session.getAttribute("username") != null) {
return "dashboard";
} else {
return "redirect:/login";
}
}
@GetMapping("/logout")
public String logout(HttpSession session) {
session.removeAttribute("username");
return "redirect:/login";
}
}
```
这个类中有 3 个方法:
- login():处理 GET 请求,返回登录页面。
- doLogin():处理 POST 请求,判断用户名和密码是否正确,若正确则将用户名存入 session 中并跳转到仪表盘页面,若不正确则返回登录页面。
- dashboard():处理 GET 请求,判断用户是否已登录,若已登录则返回仪表盘页面,若未登录则跳转到登录页面。
- logout():处理 GET 请求,清除 session 中的用户名并跳转到登录页面。
接下来,创建两个 Thymeleaf 模板页面 login.html 和 dashboard.html,分别用于展示登录页面和仪表盘页面:
login.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="post" th:action="@{/login}">
<p>
<label for="username">Username:</label>
<input type="text" id="username" name="username"/>
</p>
<p>
<label for="password">Password:</label>
<input type="password" id="password" name="password"/>
</p>
<button type="submit">Login</button>
</form>
</body>
</html>
```
dashboard.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
</head>
<body>
<h1>Welcome, <span th:text="${session.username}"></span>!</h1>
<a href="/logout">Logout</a>
</body>
</html>
```
最后,启动应用程序并访问 http://localhost:8080/login 即可看到登录页面。输入正确的用户名和密码后,会跳转到仪表盘页面。如果在仪表盘页面点击 Logout,则会返回到登录页面。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)