以下是AJAX代码,我在下拉列表中选择一个值,然后从控制器返回一个List。当我返回列表时,我想使用AJAX结果更新c:forEach项。
AJAX
<script>
$(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 = <c:out value="${salary}"/>
</script>
控制器
@ResponseBody @RequestMapping(value = "/byCollege", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public List<OneStudentResult> filterByColl(@RequestBody OneStudentResult oneStudentResult, ModelMap model){
ModelAndView modelAndView = new ModelAndView();
String deptName = null;
nameList = resultService.getStudentByColl(oneStudentResult.getName(), oneStudentResult.getCollName());
Iterator<OneStudentResult> itr = nameList.iterator();
Set<String> uniqueDeptList = new TreeSet<String>();
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<String> uniqueDeptList = new TreeSet<String>();
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":"B.E. Electronics and Communication Engineering","results":null,"subjCode":null,"subjName":null,"grade":null,"result":null}]
答案 0 :(得分:1)
作为答案粘贴,更容易格式化,我怀疑需要进行两项更改,一,从响应中删除@ResponseBody
并将控制器方法更改为
public ModelAndView filterByColl(@RequestBody Mapping oneStudentResult, ModelMap model){
ModelAndView modelAndView = new ModelAndView("nameResult");
...
return modelAndView;
}
然后在ajax请求中设置适当的类型,因为响应不再是json。我认为您仍将面临解析问题,因为它很可能与数据相关,但这是一种重定向到视图的方法
答案 1 :(得分:1)
我可以看到一个问题,您使用 uniqueDeptList 作为从控制器发送的列表的名称,您使用的是 var =“uniqueDeptList” 中的名称,这可能是一个问题,请尝试使用其他标识符,例如 var =“uniqueDeptListCurrentItem” 强>
<select id="byDept" name="byDept" >
<c:forEach items="${uniqueDeptList}" var="uniqueDeptListCurrentItem">
<option value="${uniqueDeptListCurrentItem}">
${uniqueDeptListCurrentItem}</option >
</c:forEach >
</select >