1

我有实体类考试,它与实体类 Student 连接为 ManyToOne。考试以ManyToOne与Subject相关联。我的 JSP 文件我向 Controller 发送了相应的输入。但是当我尝试发送它时出现错误(错误请求)。我发现了它发生的原因。在考试中,我有以下字段:考试标记、学生、科目和考试 ID。ExamId 自动生成。我在 JSP 中输入的其他字段。当我尝试使用 Ajax 传递学生的值并在控制器中接受检查时,出现错误。但是当我只通过考试标记时就可以了。我不知道为什么会这样。

@Entity
@Table(name = "Examination")
public class Examination implements Serializable {

public Examination() {}

public Examination(String examinationMark) {
    this.examinationMark = examinationMark;
}

// create connectivity with table Student
private Student student;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "StudentID")
public Student getStudent() {
    return this.student;
}

public void setStudent(Student student) {
    this.student = student;
}

// create connectivity with table Subject
private Subject subject;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "SubjectId")
public Subject getSubject() {
    return subject;
}

public void setSubject(Subject subject) {
    this.subject = subject;
}

学科实体类

@Entity
@Table(name = "Subject")
public class Subject implements Serializable {
public Subject() {}

public Subject(String subjectTitle, int subjectHours) {
    this.subjectTitle = subjectTitle;
    this.subjectHours = subjectHours;
}

// create connectivity with table Examination
private Set<Examination> examinations; 

@OneToMany(mappedBy = "subject", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Examination> getExaminations() {
    return examinations;
}

public void setExaminations(Set<Examination> examinations) {
    this.examinations = examinations;
}

学生实体类

@Entity
@Table(name = "Student")
public class Student implements Serializable {

public Student() {}

public Student(String studentFullName, String studentBook,
               int studentEnter, String studentOKR) {
    this.studentFullName = studentFullName;
    this.studentBook = studentBook;
    this.studentEnter =studentEnter;
    this.studentOKR = studentOKR;
}

// create connectivity with table Examination
private Set<Examination> examinations = new HashSet<Examination>();

@OneToMany(mappedBy = "student",cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Examination> getExaminations() {
    return examinations;
}

public void setExaminations(Set<Examination> examinations) {
    this.examinations = examinations;
}

控制器的方法

@RequestMapping(value = "/studentProfileEdit.html", method = RequestMethod.GET)
public ModelAndView getStudentProfile() {

    ModelAndView mav = new ModelAndView("studentProfileEdit"); // create MVC object
                                                           // to pass it to JSP page

    mav.getModelMap().put("student", sts.selectStudentByName("name"));
    return mav;
}

@RequestMapping(value = "/studentProfileEdit.html", method = RequestMethod.POST)
public @ResponseBody String editStudentProfile( @ModelAttribute(value = "examination") Examination examination) {

return "";
}

JSP 文件

<div id="examPart">
                <label>Subject</label>
                <select id="subject">
                    <c:forEach var="s" items="${subjects}"> 
                        <option value="${s.subjectTitle}" >${s.subjectTitle}</option>
                    </c:forEach>
                </select>
                    <br/>
                <label>Exam mark</label>
                <input id="examinationMark" />
                <input type="submit" value="Add exam" onclick="addExam()" />
                <div id="exam" style="color:green"></div>
            </div>

和 Ajax 函数

function addExam() {
            var examinationMark = $('#examinationMark').val();
            var subject = $('#subject');
            var student = '${student}';

            $.ajax({
                type: "POST",
                url: "/IRSystem/studentProfileEdit.html",
                data: "examinationMark=" + examinationMark +
                      "&student=" + student +
                      "&subject=" + subject ,
                success: function(response) {
                    $('#exam').html(response);
                    $('#examinationMark').val('');
                },
                error: function(e) {
                    alert('Error' + e);
                }

            });
        } 
4

0 回答 0