Following is the AJAX code where I select a value in dropdown and I get a List back from controller. When I get the list back, I want to update the c:forEach item using the AJAX result.
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>
Controller
@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";
}
Actually the purpose of the AJAX call is to update the in jstl of jsp using AJAX.
PS: As of now I could see my old table going away and I see this my List as follows
[{"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}]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…