简体   繁体   English

Spring MVC传递模型以查看然后传递给控制器

[英]Spring MVC pass model to view then to controller

I'm stuck with my code, I'm trying to pass the info of the object "Student". 我坚持使用我的代码,试图传递对象“ Student”的信息。 My scenario is like this: 我的情况是这样的:

  1. Registration Form (fill the details then press submit button go to next page) 注册表(填写详细信息,然后按提交按钮进入下一页)

  2. From this view the model will be printed out then press the next button again. 从该视图中,将打印出模型,然后再次按下一步按钮。

  3. This third page will just show the information again. 第三页将再次显示该信息。

Q: How can i pass the same object and display it to other views? 问:如何传递同一对象并将其显示到其他视图?

My code is like this. 我的代码是这样的。

Registration view: 注册视图:

<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>

First Information View: 第一信息视图:

<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>

Second Information View: 第二信息视图:

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

My Controller: 我的控制器:

@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;
}

In attempting to re-display the information from second view to third view the object Student is not being passed. 在尝试将信息从第二视图重新显示到第三视图时,未传递对象Student。

There are no fields to submit in your fist information view. 在拳头信息视图中没有可提交的字段。 You have to add the values to hidden fileds: 您必须将值添加到隐藏的文件中:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM