使用AJAX和spring MVC,如何从Spring Controller中返回对象列表并使用Jquery显示它们。使用Spring MVC和ajax来处理对象列表
,让AJAX请求如下:
$.ajax({
type: "POST",
url: "allUser.html",
dataType:'json',
data: "select=" + selectedCheckboxArray,
success: function(data){
var userListContent="";
var json =data.message;
$.each(json, function(i, obj) {
userListContent=userListContent+"
";userListContent=userListContent+"
";userListContent=userListContent+"
"+obj.firstName+" "+obj.lastName +"";userListContent=userListContent+"
"+ obj.gender +"";userListContent=userListContent+"
"+ obj.userName +" ";userListContent=userListContent+"
"+ obj.userType +"";userListContent=userListContent+"
"+ obj.status +"";userListContent=userListContent+"
"+ obj.emailId +"";userListContent=userListContent+"
"+ obj.address +"";userListContent=userListContent+"
"+ obj.contactNo +"";userListContent=userListContent+"
";});
$('#rounded-corner tbody').html(userListContent);
//console.log(userListContent);
},
error: function(e){
alert('Error: ' + e.responseText);
}
});
MVC Contrller
@RequestMapping(value="/deleteUser",method= RequestMethod.POST)
public @ResponseBody Map deleteUser(UserDetails user,HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
System.out.println("Ajax Request Received for delete User...............");
Map model = new HashMap();
JsonResponse js=new JsonResponse();
js.setResult("pass");
js.setStatus("active");
// String operation=request.getParameter("operation");
String[] selectedUserIdParameter = request.getParameterValues("select");
System.out.println("Length:"+selectedUserIdParameter.length);
/* Code Description:
* Array "selectedUserIdParameter" above has ID like {1,2,3,.....},
* we need to use array like {1 2 3 4 } without (,).so first we must convert.
* Following code doing the same.
* After Conversion Array "selectedUserId" will have ID like {1 2 3 4 }
* If You Know PHP explode()" function ,following is doing something like what explode() function does .
*/
String msg="hello";
List usersList = userService.getAllUser();
int no=usersList.size();
System.out.println("Size:"+no);
model.put("message", usersList);
model.put("jso", js);
return model;
}
+1
请正确地格式化代码,现在它很不可读 –
+2
你是什么问题或问题? –