springboot实现教师登录功能,登录成功显示学生列表页面,失败提示账号密码错误。
时间: 2024-03-25 10:38:30 浏览: 18
好的,这是一个较为简单的问题。你可以按照以下步骤实现:
1. 创建教师表
首先,我们需要创建一个教师表,用于存储教师的信息,包括账号和密码等信息。可以通过MySQL等数据库来创建该表,例如:
```
CREATE TABLE teacher (
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
```
2. 配置Spring Security
在Spring Boot中,我们可以使用Spring Security来实现登录功能。在配置Spring Security时,我们需要指定登录页面、登录成功后的跳转页面、用户名和密码的字段名等信息。可以在application.properties或application.yml中添加如下配置:
```
# 配置登录页面和登录成功后的跳转页面
spring.security.login-page=/login
spring.security.default-target-url=/student/list
# 配置用户名和密码的字段名
spring.security.user.name=username
spring.security.user.password=password
# 配置教师账号密码
spring.security.user.password=teacher:123456
```
3. 创建登录页面
在templates目录下创建login.html文件,该文件用于显示登录页面。可以使用Bootstrap等前端框架来美化页面,例如:
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form method="post" action="/login">
<div class="form-group">
<label for="username">账号:</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<button type="submit" class="btn btn-primary">登录</button>
</form>
</div>
</body>
</html>
```
4. 创建学生列表页面
在templates目录下创建student-list.html文件,该文件用于显示学生列表页面。可以使用Thymeleaf等模板引擎来动态渲染页面,例如:
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生列表页面</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>学生列表</h1>
<table class="table">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>班级</th>
</tr>
</thead>
<tbody>
<!-- 使用Thymeleaf动态渲染表格数据 -->
<tr th:each="student : ${students}">
<td th:text="${student.id}"></td>
<td th:text="${student.name}"></td>
<td th:text="${student.className}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
```
5. 创建Controller
在Controller中添加@RequestMapping注解,用于处理登录请求和显示学生列表页面请求。例如:
```
@Controller
public class LoginController {
@Autowired
private TeacherRepository teacherRepository;
@Autowired
private StudentRepository studentRepository;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "login";
}
@RequestMapping(value = "/student/list", method = RequestMethod.GET)
public String studentList(Model model) {
List<Student> students = studentRepository.findAll();
model.addAttribute("students", students);
return "student-list";
}
}
```
6. 运行应用程序
最后,运行应用程序并访问http://localhost:8080/login,就可以看到登录页面了。输入正确的账号和密码,就可以跳转到学生列表页面了。如果账号或密码不正确,会提示错误信息。
以上是一个简单的实现教师登录并显示学生列表页面的过程,具体实现可能还需要根据实际情况进行调整。