任务七、八

main
董倩 2 weeks ago
parent 76a63f4128
commit c59decf6af

@ -17,19 +17,19 @@ public class CourseController {
// 基本数据验证
if (course.getCourseId() == null || course.getCourseId() <= 0) {
model.addAttribute("msg", "课程ID不能为空");
return "/addCourse.jsp";
return "addCourse";
}
if (course.getCourseName() == null || course.getCourseName().trim().length() <= 0) {
model.addAttribute("msg", "课程名称不能为空!");
return "/addCourse.jsp";
return "addCourse";
}
if (course.getCredit() == null || course.getCredit() <= 0) {
model.addAttribute("msg", "课程学分不能为空!");
return "/addCourse.jsp";
return "addCourse";
}
if (course.getTeacher() == null || course.getTeacher().trim().length() <= 0) {
model.addAttribute("msg", "课程教师不能为空!");
return "/addCourse.jsp";
return "addCourse";
}
model.addAttribute("msg", "课程信息提交成功!");
@ -37,7 +37,7 @@ public class CourseController {
} catch (Exception e) {
System.err.println("数据处理异常:" + e.getMessage());
model.addAttribute("msg", "数据提交失败,请检查输入格式!");
return "/addCourse.jsp";
return "addCourse";
}
}
@ -57,7 +57,7 @@ public String submitCourse(Course course, Model model) {
} catch (Exception e) {
System.err.println("数据处理异常:" + e.getMessage());
model.addAttribute("msg", "数据提交失败,请检查输入格式!");
return "/addCourse.jsp";
return "addCourse";
}
}
@ -69,7 +69,7 @@ public String submitCourse(Course course, Model model) {
public String handleException(Exception e, Model model) {
System.err.println("系统异常:" + e.getMessage());
model.addAttribute("msg", "系统错误:" + e.getMessage());
return "showCourse";
return "addCourse";
}
}

@ -0,0 +1,73 @@
package com.ssm.controller;
import com.ssm.entity.Course;
import com.ssm.entity.Score;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ScoreController {
@RequestMapping("/addScore")
public String add(Score score, Model model) {
try {
System.out.println("===== 组员董倩-成绩模块 =====");
System.out.println("接收数据:" + score);
// 基本数据验证
if (score.getStudentId() == null || score.getStudentId().trim().length() <= 0) {
model.addAttribute("msg", "学生ID不能为空");
return "redirect:/addScore";
}
if (score.getCourseName() == null || score.getCourseName().trim().length() <= 0) {
model.addAttribute("msg", "课程名称不能为空!");
return "redirect:/addScore";
}
if (score.getGrade() == null || score.getGrade() < 0 || score.getGrade() > 100) {
model.addAttribute("msg", "成绩必须在0-100之间");
return "redirect:/addScore";
}
if (score.getSemester() == null || score.getSemester().trim().length() <= 0) {
model.addAttribute("msg", "学期不能为空!");
return "redirect:/addScore";
}
model.addAttribute("msg", "成绩信息提交成功!");
model.addAttribute("score", score);
return "showScore";
} catch (Exception e) {
System.err.println("数据处理异常:" + e.getMessage());
model.addAttribute("msg", "数据提交失败,请检查输入格式!");
return "redirect:/addScore";
}
}
// ... existing code ...
// 新增方法:接收表单信息并响应到页面
@RequestMapping("/submitScore")
public String submitScore(Score score, Model model) {
try {
System.out.println("===== 成绩提交 =====");
System.out.println("接收数据:" + score);
// 将表单信息添加到Model中传递到页面
model.addAttribute("score", score);
return "showScore";
} catch (Exception e) {
System.err.println("数据处理异常:" + e.getMessage());
model.addAttribute("msg", "数据提交失败,请检查输入格式!");
return "redirect:/addScore";
}
}
// 全局异常处理 - 处理数据绑定异常
@ExceptionHandler(Exception.class)
public String handleException(Exception e, Model model) {
System.err.println("系统异常:" + e.getMessage());
model.addAttribute("msg", "系统错误:" + e.getMessage());
return "redirect:/addScore";
}
}

@ -0,0 +1,60 @@
package com.ssm.entity;
public class Score {
private String studentId;
private String courseName;
private Double grade;
private String semester;
public Score() {
}
public Score(String studentId, String courseName, Double grade, String semester) {
this.studentId = studentId;
this.courseName = courseName;
this.grade = grade;
this.semester = semester;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Double getGrade() {
return grade;
}
public void setGrade(Double grade) {
this.grade = grade;
}
public String getSemester() {
return semester;
}
public void setSemester(String semester) {
this.semester = semester;
}
@Override
public String toString() {
return "Score{" +
"studentId='" + studentId + '\'' +
", courseName='" + courseName + '\'' +
", grade=" + grade +
", semester='" + semester + '\'' +
'}';
}
}

@ -0,0 +1,34 @@
<%--
Created by IntelliJ IDEA.
User: dqq
Date: 2026/5/19
Time: 11:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>提交成功</title>
</head>
<body>
<h3>Success!</h3>
<table>
<tr>
<td>学生ID</td>
<td>${score.studentId}</td>
</tr>
<tr>
<td>课程姓名:</td>
<td>${score.courseName}</td>
</tr>
<tr>
<td>成绩:</td>
<td>${score.grade}</td>
</tr>
<tr>
<td>学期:</td>
<td>${score.semester}</td>
</tr>
</table>
</body>
</html>

@ -0,0 +1,25 @@
<%--
Created by IntelliJ IDEA.
User: dqq
Date: 2026/5/19
Time: 11:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成绩信息</title>
</head>
<body>
<h3>成绩信息</h3>
<form action="addScore" method="post">
学生ID<input type="text" name="studentId" placeholder="请输入学生ID"><br>
课程名称:<input type="text" name="courseName" placeholder="请输入课程名称"><br>
成绩:<input type="text" name="grade" placeholder="请输入成绩"><br>
学期:<input type="text" name="semester" placeholder="请输入第几学期"><br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
Loading…
Cancel
Save