【问题标题】:unable to bind ModelMap through ajax call in Spring MVC无法通过 Spring MVC 中的 ajax 调用绑定 ModelMap
【发布时间】:2014-12-17 08:39:17
【问题描述】:

我的 JSP 页面:

<form action="manageparentexamforchildren.htm" class="form-horizontal form-bordered" >
              <div class="form-body">
                <div class="form-group">
                   <label class="control-label col-md-1"><spring:message code="label.parent.children"/></label>
                              <div class="col-md-3">
                                <select name="parent.children" class="form-control" id="children" required="true" >
                                    <option value="">-<spring:message code="label.select"/>-</option>
                                    <c:forEach items="${studentsOfThisParent}" var="student">
                                       <option value="${student.id}">${student.firstName} ${student.lastName}</option>
                                    </c:forEach>
                                </select>
                              </div>
                </div>

                </div>
              <div class="form-actions fluid">
                <div class="row">
                  <div class="col-md-12">
                    <div class="col-md-offset-3 col-md-9">
                      <button type="submit" class="btn blue"><i class="icon-ok"></i><spring:message code='button.search'/></button>
                      <button type="button" onclick="javascript:history.go(-1);" class="btn red default"><spring:message code='button.cancel'/></button>
                    </div>
                  </div>
                </div>
              </div>
            </form>


            <c:forEach items="${modelList}" var="examination">
    <tr>
      <td><b><spring:message code="label.examination.schoolexam"/></b>:<c:out value="${examination.schoolExam.name}" /></td>
    </tr>

    <table border="1" width="1000" height="200" class="table table-bordered table-hover"> 
   <thead> <tr><th class="active"><spring:message code="label.examination.subjects"/></th><th class="active"><spring:message code="label.exam.Date"/></th><th class="active"><spring:message code="label.exam.maxmarks"/></th></tr></thead>
    <tbody>
    <c:forEach items="${examination.examinationSubjects}" var="examinationSubject" varStatus="status">
    <tr>
    <td class="success"><c:out value="${examinationSubject.subjects.name}" /></td>
    <td class="success" ><c:out value="${examinationSubject.date}" /></td>
    <td class="success"><c:out value="${examinationSubject.maxMarks}" /></td>

    </tr>
    </c:forEach>
    </tbody>
    </table>

  </c:forEach>

我的控制器:

@RequestMapping(value="/manageparentexam.htm", method=RequestMethod.GET)
    public String manageparentexam(ModelMap modelMap,HttpServletRequest request )
    {

        Parent parent = parentService.getParentById(getLoggedInUser().getId());
        List <Student> studentsOfThisParent=parentService.getStudentsOfThisParent(getLoggedInUser().getId());
        Student student=null;

            student=parent.getStudentAdmission().get(0).getStudent();
            modelMap.addAttribute("modelList",student.getStudentAdmission().getClasses().getExamination());


        setupCreate(modelMap, request);
        return "tileDefinition.manageparentexam-"+getNameSpace();
    }



    @RequestMapping(value="/manageparentexamforchildren.htm", method=RequestMethod.GET)
        public void manageparentexamforchildren(ModelMap modelMap,HttpServletRequest request )
        {
            Long studentId=Long.valueOf(request.getParameter("parent.children"));
            modelMap.addAttribute("modelList",studentService.getById(studentId).getStudentAdmission().getClasses().getExamination());
            setupCreate(modelMap, request);
         }

这里我的要求是,每当我在选择框中选择一个值,然后表格应该通过 ajax 更新,这里我无法通过 ajax 调用绑定模型列表。 . .

【问题讨论】:

  • 您所说的:“通过 ajax 调用绑定模型列表”是什么意思,“我无法”是什么意思——发生了什么(您希望发生什么)?

标签: java jquery ajax spring spring-mvc


【解决方案1】:

我要做的是在另一个jsp中分离表格。 yourTable.jsp 像这样

        <table border="1" width="1000" height="200" class="table table-bordered table-hover"> 
             <thead> <tr><th class="active"><spring:message code="label.examination.subjects"/></th><th class="active"><spring:message code="label.exam.Date"/></th><th class="active"><spring:message code="label.exam.maxmarks"/></th></tr></thead>
<tbody>
     <c:forEach items="${examination.examinationSubjects}" var="examinationSubject" varStatus="status">
<tr>
<td class="success"><c:out value="${examinationSubject.subjects.name}" /></td>
<td class="success" ><c:out value="${examinationSubject.date}" /></td>
<td class="success"><c:out value="${examinationSubject.maxMarks}" /></td>

</tr>
</c:forEach>
</tbody>
</table>

然后你将这个jsp包含在你的主映射中的一个div中

    <div id="yourTable"> 
       <c:jsp include="yourTable.jsp/>
   </div>

然后,在您的选择中,您必须添加一个侦听器,当某些事情发生变化时,您需要进行 JQuery 加载以重新加载表格,并再次从控制器呈现表格并进行所有您想要的更改

              $('#yourTable').load("url of your controller where you will render yourTable.jsp");

在控制器中,您将呈现您的Table.jsp,您将在 ModelAndView 对象中添加考试主题

【讨论】:

    猜你喜欢
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2018-05-29
    • 2020-10-02
    相关资源
    最近更新 更多