Reputation: 360
I want generate AJAX (based on jQuery) request to Spring MVC controller and return Collection with objects to jsp. I have two troubles: 1. AJAX success block doesn't work (something with data type); 2. Don't know how to display this objects to the jsp.
<c:forEach items="${objects}" var="object">
size : <c:out value="${object.size}" />
</c:forEach>
^ This code doesn't work.
My AJAX request:
function AJAXItemRequest(name, surname) {
$.ajax({
type : "POST",
dataType: "json",
url : "/pages/users",
data : {name: nameValue, surname: surnameValue},
success : function(data) {
alert('success');
}
});
}
Spring MVC Controller
@RequestMapping(value = "/users", method = RequestMethod.POST)
public ModelMap userHandler(ModelMap model, HttpServletRequest request, @RequestParam("name") String nameValue, @RequestParam("surname") String surnameValue ) throws Exception {
Collection<Users> users = findConnectedUsers(name, surname);
model.addAttribute("objects", users);
return model;
}
Also, AJAX request works and controller successfully returns model Collection.
Upvotes: 0
Views: 3075
Reputation: 2587
This is not working , To use Ajax with Spring you should use respose body and not ModelView
@RequestMapping(value="/searchAlluserByQuery.do", method = RequestMethod.POST,headers="content-type=application/json")
@ResponseBody public List<Object> searchAlluserByQuery(@RequestBody String query) {
return serachService.getListByqueryContains(query);
}
Don't forget have jackson.jar on your class path and in view layer Jstl not working for ajax! you should use java script frame work such jquery to get Json object
Upvotes: 1