SpringMVC,Controller如何接收前端传递过来的数组


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!");
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值