新建模块

新建包和类

相关代码如下
UserController
package com.itheima.controller;
import com.itheima.entity.User;
import com.itheima.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class UserController {
@Autowired
private UserRepository userRepository;
private final String adminUser = "CSS";
private final String adminPass = "111";
@GetMapping("/")
public String showLoginPage() {
return "login";
}
@PostMapping("/login")
public String login(@RequestParam String username,
@RequestParam String password,
Model model) {
if ("CSS".equals(username) && "111".equals(password)) {
model.addAttribute("username", username);
return "success";
}
User user = userRepository.findByUsername(username);
if (user != null && user.getPassword().equals(password)) {
model.addAttribute("username", user.getUsername());
return "success";
}
model.addAttribute("error", "用户名或密码错误!");
return "login";
}
@GetMapping("/register")
public String showRegisterPage() {
return "register";
}
@PostMapping("/register")
public String register(@RequestParam String username,
@RequestParam String password,
Model model) {
if (username.equals(adminUser)) {
model.addAttribute("error", "管理员账号不可注册!");
return "register";
}
if (userRepository.findByUsername(username) != null) {
model.addAttribute("error", "该用户名已存在!");
return "register";
}
User newUser = new User();
newUser.setUsername(username);
newUser.setPassword(password);
userRepository.save(newUser);
model.addAttribute("msg", "注册成功,请返回登录!");
return "register";
}
}
User
package com.itheima.entity;
import jakarta.persistence.*;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserRepository
```java
package com.itheima.repository;
import com.itheima.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
前端页面
login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>用户登录</title>
<style>
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #6a11cb, #2575fc);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
}
.login-box {
background-color: rgba(255, 255, 255, 0.95);
padding: 40px;
border-radius: 12px;
box-shadow: 0 8px 20px rgba(0,0,0,0.2);
text-align: center;
width: 320px;
}
h2 {
margin-bottom: 30px;
color: #333;
font-size: 28px;
}
input {
width: 90%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 6px;
font-size: 16px;
}
button {
width: 95%;
padding: 10px;
margin: 10px 0;
border: none;
border-radius: 6px;
background-color: #2575fc;
color: white;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
}
button:hover {
background-color: #6a11cb;
}
.register-btn {
background-color: #fff;
color: #2575fc;
border: 1px solid #2575fc;
}
.register-btn:hover {
background-color: #2575fc;
color: white;
}
.error {
color: red;
font-size: 14px;
}
</style>
</head>
<body>
<div class="login-box">
<h2>用户登录</h2>
<form th:action="@{/login}" method="post">
<input type="text" name="username" placeholder="用户名" required/><br/>
<input type="password" name="password" placeholder="密码" required/><br/>
<button type="submit">登录</button>
</form>
<form th:action="@{/register}" method="get">
<button type="submit" class="register-btn">注册</button>
</form>
<p th:text="${error}" class="error"></p>
</div>
</body>
</html>
register.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>用户注册</title>
<style>
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #ff9a9e, #fad0c4);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
}
.register-box {
background-color: rgba(255, 255, 255, 0.95);
padding: 40px;
border-radius: 12px;
box-shadow: 0 8px 20px rgba(0,0,0,0.2);
text-align: center;
width: 320px;
}
h2 {
margin-bottom: 30px;
color: #333;
font-size: 28px;
}
input {
width: 90%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 6px;
font-size: 16px;
}
button {
width: 95%;
padding: 10px;
margin: 10px 0;
border: none;
border-radius: 6px;
background-color: #ff6a88;
color: white;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
}
button:hover {
background-color: #ff99ac;
}
.back-btn {
background-color: #fff;
color: #ff6a88;
border: 1px solid #ff6a88;
}
.back-btn:hover {
background-color: #ff6a88;
color: white;
}
.error {
color: red;
font-size: 14px;
}
.msg {
color: green;
font-size: 14px;
}
</style>
</head>
<body>
<div class="register-box">
<h2>用户注册</h2>
<form th:action="@{/register}" method="post">
<input type="text" name="username" placeholder="用户名" required/><br/>
<input type="password" name="password" placeholder="密码" required/><br/>
<button type="submit">注册</button>
</form>
<form th:action="@{ / }" method="get">
<button type="submit" class="back-btn">返回登录</button>
</form>
<p th:text="${error}" class="error"></p>
<p th:text="${msg}" class="msg"></p>
</div>
</body>
</html>
success.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>登录成功</title>
<style>
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #6a11cb, #2575fc);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
}
.success-box {
background-color: rgba(255, 255, 255, 0.95);
padding: 40px;
border-radius: 12px;
box-shadow: 0 8px 20px rgba(0,0,0,0.2);
text-align: center;
}
h2 {
color: green;
font-size: 28px;
}
</style>
</head>
<body>
<div class="success-box">
<h2>🎉🎉恭喜你,<span th:text="${username}"></span>,登录成功!🎉🎉</h2>
</div>
</body>
</html>
注册成功界面

登陆成功界面
