Spring MVC 将模型传递给视图然后传递给控制器

Spring MVC pass model to view then to controller

我受困于我的代码,我正在尝试传递对象的信息 "Student"。我的场景是这样的:

  1. 注册表(填写详细信息然后按提交按钮转到下一页)

  2. 从这个视图中,模型将被打印出来,然后再次按下一个按钮。

  3. 第三页只是再次显示信息。

问:如何传递同一个对象并显示到其他视图?

我的代码是这样的

注册查看:

<form action="/HamburgerProject/stuSubmitAdmissionForm.html" method="post">
    <table>
    <tr>
        <td>Name:</td>          <td><input type="text" name="studentName"></td></tr>
    <tr>
        <td>Age:</td>           <td><input type="text" name="studentAge"></td></tr>
    <tr>
    </table>
    <input type="submit" value="submit this">
</form>

第一个信息查看:

<form action="/HamburgerProject/stuSubmitAdmissionForm.html" method="post">
<table>
    <tr>
        <td>Student's Name :</td>
        <td>${student.studentName}</td>
    </tr>
    <tr>
        <td>Student's Age :</td>
        <td>${student.studentAge}</td>
    </tr>
</table>
    <input type="submit" value="send"/>
</form>

第二个信息视图:

<table>
    <tr>
        <td>Student's Name :</td>
        <td>${student.studentName}</td>
    </tr>
    <tr>
        <td>Student's Age :</td>
        <td>${student.studentAge}</td>
    </tr>
</table>

我的控制器:

@RequestMapping(value="/stuAdmissionForm.html", method = RequestMethod.GET)
public ModelAndView getStudentAdmissionForm() {
    ModelAndView model = new ModelAndView("AdmissionForm");
    return model;

}

@RequestMapping(value="/stuSubmitAdmissionForm.html", method = RequestMethod.POST) 
public ModelAndView submitModelAttributeAnnotationAdmissionForm(@ModelAttribute("student") Student student) {

    ModelAndView model = new ModelAndView("AdmissionSuccess");
    return model;
}

@RequestMapping(value="/stuDisplayForm.html", method = RequestMethod.POST) 
public ModelAndView getStudent(Student student) {

    ModelAndView model = new ModelAndView("NewForm");
    model.addObject(student);

    return  model;
}

在尝试将信息从第二个视图重新显示到第三个视图时,未传递对象 Student。

您的拳头信息视图中没有可提交的字段。您必须将值添加到隐藏字段:

<form action="/HamburgerProject/stuSubmitAdmissionForm.html" method="post">
<table>
    <tr>
        <td>Student's Name :</td>
        <td>${student.studentName}</td>
    </tr>
    <tr>
        <td>Student's Age :</td>
        <td>${student.studentAge}</td>
    </tr>
</table>
    <input type="hidden" name="studentName" value="${student.studentName}">
    <input type="hidden" name="studentAge" value="${student.studentAge}">
    <input type="submit" value="send"/>
</form>