1,前端页面:传递过去一个数组
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery1.9.min.js"></script>
</head>
<body>
<input type="button" οnclick="textAjax()" value="测试ajax">
</body>
<script type="text/javascript">
function textAjax() {
var user1 = {
username : 'zs',
password : '123'
};
var user2 = {
username : 'lisi',
password : '234'
};
var person = {
name : 'zhaoliu',
password : '345'
};
var user = [];
user.push(user1);
user.push(user2);
user.push(person);
var userJson = JSON.stringify(user);
$.ajax({
type : 'post',
url : 'user/login3.action',
dataType : 'json',
contentType : 'application/json;charset=utf-8',
data : userJson,
success : function(result) {
alert(result);
}
});
}
</script>
</html>
2,conrtroller接收:
package cn.gzsxt.controller;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.gzsxt.pojo.User;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/login.action")
@ResponseBody
public String login(User user) {
System.out.println(user.getUsername());
return new Gson().toJson("success!");
}
//接收相同类型数据的数组
@RequestMapping("/login2.action")
@ResponseBody
public String login2(@RequestBody String userJson) {
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(
List.class, User.class);
try {
List<User> list = mapper.readValue(userJson, javaType);
System.out.println(list.toString());
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return new Gson().toJson("success!");
}
//list<Object>接收数组中存在不同类型的数据
@RequestMapping("/login3.action")
@ResponseBody
public String login3(@RequestBody List<Object> list) throws IllegalArgumentException, IllegalAccessException {
for(int i = 0 ; i <list.size();i++){
Object obj = list.get(i);
System.out.println(obj);
}
return new Gson().toJson("success!");
}
}