springboot实现网页端登录和增删改查代码
时间: 2023-06-17 14:02:12 浏览: 78
以下是一个简单的Spring Boot实现网页端登录和增删改查的示例代码:
1. 首先,我们需要创建一个用户实体类 `User.java`:
```java
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String fullName;
// getters and setters
}
```
2. 创建一个用户仓库类 `UserRepository.java`:
```java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByEmail(String email);
}
```
3. 创建一个服务类 `UserService.java`:
```java
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User findByEmail(String email) {
return userRepository.findByEmail(email);
}
public void save(User user) {
userRepository.save(user);
}
public void delete(User user) {
userRepository.delete(user);
}
public List<User> findAll() {
return userRepository.findAll();
}
}
```
4. 创建一个控制器类 `UserController.java`:
```java
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/")
public String index(Model model) {
model.addAttribute("users", userService.findAll());
return "index";
}
@GetMapping("/login")
public String loginForm() {
return "login";
}
@PostMapping("/login")
public String login(@RequestParam String email, @RequestParam String password, HttpSession session) {
User user = userService.findByEmail(email);
if (user != null && user.getPassword().equals(password)) {
session.setAttribute("user", user);
return "redirect:/";
} else {
return "redirect:/login";
}
}
@GetMapping("/logout")
public String logout(HttpSession session) {
session.removeAttribute("user");
return "redirect:/";
}
@GetMapping("/register")
public String registerForm(Model model) {
model.addAttribute("user", new User());
return "register";
}
@PostMapping("/register")
public String register(@Valid User user, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "register";
} else {
userService.save(user);
return "redirect:/login";
}
}
@GetMapping("/edit/{id}")
public String editForm(@PathVariable Long id, Model model) {
User user = userService.findById(id);
if (user != null) {
model.addAttribute("user", user);
return "edit";
} else {
return "redirect:/";
}
}
@PostMapping("/edit")
public String edit(@Valid User user, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "edit";
} else {
userService.save(user);
return "redirect:/";
}
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable Long id) {
User user = userService.findById(id);
if (user != null) {
userService.delete(user);
}
return "redirect:/";
}
}
```
5. 创建网页模板文件 `index.html`,`login.html`,`register.html`,`edit.html`:
`index.html`
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Management</title>
</head>
<body>
<h1>User Management</h1>
<p><a href="/logout">Logout</a></p>
<table>
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>Full Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.email}"></td>
<td th:text="${user.fullName}"></td>
<td>
<a th:href="@{/edit/{id}(id=${user.id})}">Edit</a>
<a th:href="@{/delete/{id}(id=${user.id})}">Delete</a>
</td>
</tr>
</tbody>
</table>
<p><a href="/register">Register</a></p>
</body>
</html>
```
`login.html`
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="post" action="/login">
<label>Email:</label><br>
<input type="email" name="email"><br>
<label>Password:</label><br>
<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
<p><a href="/register">Register</a></p>
</body>
</html>
```
`register.html`
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form method="post" action="/register">
<label>Email:</label><br>
<input type="email" name="email" th:field="*{email}"><br>
<label>Password:</label><br>
<input type="password" name="password" th:field="*{password}"><br>
<label>Full Name:</label><br>
<input type="text" name="fullName" th:field="*{fullName}"><br>
<input type="submit" value="Register">
</form>
<p><a href="/login">Login</a></p>
</body>
</html>
```
`edit.html`
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edit User</title>
</head>
<body>
<h1>Edit User</h1>
<form method="post" action="/edit">
<input type="hidden" name="id" th:field="*{id}">
<label>Email:</label><br>
<input type="email" name="email" th:field="*{email}"><br>
<label>Password:</label><br>
<input type="password" name="password" th:field="*{password}"><br>
<label>Full Name:</label><br>
<input type="text" name="fullName" th:field="*{fullName}"><br>
<input type="submit" value="Save">
</form>
<p><a href="/">Back to List</a></p>
</body>
</html>
```
6. 最后,在 `application.properties` 文件中设置数据库连接和端口:
```
spring.datasource.url=jdbc:mysql://localhost:3306/user_management?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
server.port=8080
```
这样我们就完成了一个简单的Spring Boot实现网页端登录和增删改查的示例代码。