如何在Spring项目中使用Ajax更新ac:forEach?

以下是AJAX代码,我在下拉列表中选择一个值,然后从控制器返回一个List。 当我返回列表时,我想使用AJAX结果更新c:forEach项。

AJAX

 $(document).ready(function() { $("#byCollege").change(function() { // Get and convert the data for sending // Example: This variable contains the selected option-text var collName = $('#byCollege').val(); var studentName = '${salary}'; var json = {"name" : studentName, "collName" : collName}; // Send the data as an ajax POST request $.ajax({ url: "http://localhost:8080/AnnaUnivResults/mvc/byCollege", type: 'POST', dataType: 'json', data: JSON.stringify(json), contentType: "application/json", mimeType: 'application/json', success: function(response) { alert(JSON.stringify(response)); $("#studList").html(JSON.stringify(response)); }, error: function(data, status, er) { alert("error: " + data + " status: " + status + " er:" + er); } }); }); }); $(document).ready(function() { alert(document.getElementById('studentName').val()); }); var current = document.getElementById('studentName').value(); currnet.value =   

调节器

 @ResponseBody @RequestMapping(value = "/byCollege", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) public List filterByColl(@RequestBody OneStudentResult oneStudentResult, ModelMap model){ ModelAndView modelAndView = new ModelAndView(); String deptName = null; nameList = resultService.getStudentByColl(oneStudentResult.getName(), oneStudentResult.getCollName()); Iterator itr = nameList.iterator(); Set uniqueDeptList = new TreeSet(); while(itr.hasNext()){ System.out.println(itr.next().getName()); } uniqueDeptList.add(" Select a Department "); model.addAttribute("uniqueDeptList", uniqueDeptList); model.addAttribute("nameList", nameList); modelAndView.addObject("nameList", nameList); return nameList; } Set uniqueDeptList = new TreeSet(); while(itr.hasNext()){ deptName = itr.next().getDeptName(); if(!uniqueDeptList.contains(deptName)){ uniqueDeptList.add(deptName); } } uniqueDeptList.add(" Select a Department "); model.addAttribute("uniqueDeptList", uniqueDeptList); model.addAttribute("nameList", nameList); return "nameResult"; } 

实际上,AJAX调用的目的是使用AJAX更新jsp的jstl。

PS:截至目前,我可以看到我的旧桌子消失,我看到我的列表如下

 [{"regNo":"1234","name":"ABCD","collName":" COLLEGE OF ENGINEERING","deptName":"BE Electronics and Communication Engineering","results":null,"subjCode":null,"subjName":null,"grade":null,"result":null}] 

作为答案粘贴,更容易格式化,我怀疑需要进行两项更改,一,从响应中删除@ResponseBody

并将控制器方法更改为类似的

  public ModelAndView filterByColl(@RequestBody Mapping oneStudentResult, ModelMap model){ ModelAndView modelAndView = new ModelAndView("nameResult"); ... return modelAndView; } 

然后在你的ajax请求中设置适当的类型,因为响应不再是json。 我认为您仍将面临解析问题,因为它很可能与数据相关,但这是一种重定向到视图的方法

我可以看到一个问题,你使用uniqueDeptList作为从控制器发送的列表的名称,你在var =“uniqueDeptList”中使用相同的名称,这可能是一个问题,尝试使用一个不同的标识符,如var =“uniqueDeptListCurrentItem”