This commit is contained in:
Chenx221 2023-06-02 14:17:44 +08:00
parent 55a124bd88
commit 93ddacdd2d
5 changed files with 196 additions and 0 deletions

View File

@ -0,0 +1,49 @@
package cyou.chenx221.controller;
import cyou.chenx221.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/admin")
public class AdminController {
private final AdminService adminService;
@Autowired
public AdminController(AdminService adminService) {
this.adminService = adminService;
}
@GetMapping("/login")
public String showLoginForm() {
return "admin-login-form";
}
@PostMapping("/login")
public String login(@RequestParam("username") String username, @RequestParam("password") String password) {
boolean isValid = adminService.validateCredentials(username, password);
if (isValid) {
// 登录成功重定向到管理页面
return "redirect:/admin/dashboard";
} else {
// 登录失败返回登录页面并显示错误消息
return "redirect:/admin/login?error";
}
}
@GetMapping("/dashboard")
public String showDashboard() {
return "admin-dashboard";
}
@GetMapping("/logout")
public String logout() {
// 执行登出操作清除用户信息或会话信息
return "redirect:/admin/login";
}
}

View File

@ -0,0 +1,72 @@
package cyou.chenx221.controller;
import cyou.chenx221.modal.Score;
import cyou.chenx221.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/scores")
public class ScoreController {
private final ScoreService scoreService;
@Autowired
public ScoreController(ScoreService scoreService) {
this.scoreService = scoreService;
}
@GetMapping
public String getAllScores(Model model) {
List<Score> scores = scoreService.getAllScores();
model.addAttribute("scores", scores);
return "score-list";
}
@GetMapping("/{id}")
public String getScoreById(@PathVariable("id") int scoreId, Model model) {
Score score = scoreService.getScoreById(scoreId);
model.addAttribute("score", score);
return "score-details";
}
@GetMapping("/add")
public String showAddScoreForm(Model model) {
model.addAttribute("score", new Score());
return "add-score-form";
}
@PostMapping("/add")
public String addScore(@ModelAttribute("score") Score score) {
scoreService.addScore(score);
return "redirect:/scores";
}
@GetMapping("/{id}/edit")
public String showEditScoreForm(@PathVariable("id") int scoreId, Model model) {
Score score = scoreService.getScoreById(scoreId);
model.addAttribute("score", score);
return "edit-score-form";
}
@PostMapping("/{id}/edit")
public String updateScore(@PathVariable("id") int scoreId, @ModelAttribute("score") Score updatedScore) {
Score score = scoreService.getScoreById(scoreId);
score.setCourse(updatedScore.getCourse());
score.setScore(updatedScore.getScore());
// 更新其他属性
scoreService.updateScore(score);
return "redirect:/scores";
}
@GetMapping("/{id}/delete")
public String deleteScore(@PathVariable("id") int scoreId) {
scoreService.deleteScore(scoreId);
return "redirect:/scores";
}
}

View File

@ -1,4 +1,71 @@
package cyou.chenx221.controller; package cyou.chenx221.controller;
import cyou.chenx221.modal.Student;
import cyou.chenx221.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/students")
public class StudentController { public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public String getAllStudents(Model model) {
List<Student> students = studentService.getAllStudents();
model.addAttribute("students", students);
return "student-list";
}
@GetMapping("/{id}")
public String getStudentById(@PathVariable("id") int studentId, Model model) {
Student student = studentService.getStudentById(studentId);
model.addAttribute("student", student);
return "student-details";
}
@GetMapping("/add")
public String showAddStudentForm(Model model) {
model.addAttribute("student", new Student());
return "add-student-form";
}
@PostMapping("/add")
public String addStudent(@ModelAttribute("student") Student student) {
studentService.addStudent(student);
return "redirect:/students";
}
@GetMapping("/{id}/edit")
public String showEditStudentForm(@PathVariable("id") int studentId, Model model) {
Student student = studentService.getStudentById(studentId);
model.addAttribute("student", student);
return "edit-student-form";
}
@PostMapping("/{id}/edit")
public String updateStudent(@PathVariable("id") int studentId, @ModelAttribute("student") Student updatedStudent) {
Student student = studentService.getStudentById(studentId);
student.setName(updatedStudent.getName());
student.setAge(updatedStudent.getAge());
// 更新其他属性
studentService.updateStudent(student);
return "redirect:/students";
}
@GetMapping("/{id}/delete")
public String deleteStudent(@PathVariable("id") int studentId) {
studentService.deleteStudent(studentId);
return "redirect:/students";
}
} }

View File

@ -16,4 +16,6 @@ public interface AdminService {
Admin getAdminByUsername(String username); Admin getAdminByUsername(String username);
List<Admin> getAllAdmins(); List<Admin> getAllAdmins();
boolean validateCredentials(String username, String password);
} }

View File

@ -47,4 +47,10 @@ public class AdminServiceImpl implements AdminService {
public List<Admin> getAllAdmins() { public List<Admin> getAllAdmins() {
return adminDao.getAllAdmins(); return adminDao.getAllAdmins();
} }
@Override
public boolean validateCredentials(String username, String password) {
Admin admin = adminDao.getAdminByUsername(username);
return admin != null && admin.getPassword().equals(password);
}
} }