spring mvc 配置:
spring-mvc.xml需要注入一个bean
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
jsp 页面:
var array=[];
$(this).prev().find('.dataTr').each(function(){
var order={};
order.id=$(this).attr('id');
order.invoiceIdentification=2;
array.push(order);
});
$.ajax({
type:'post',
data:JSON.stringify(params),//JSON.stringify()将对象转为json字符串
dataType:'json',
contentType:'application/json',//需要进行配置
url:"",
success:function(result){
}
});
Controller层:
@RequestMapping("bathPassAccount")
@ResponseBody
public JsonResult<String> bathPassAccount(@RequestBody JSONArray jsonArray){//@RequestBody得到json字符串
List<DnOrderDeliveryNote> list = (List<DnOrderDeliveryNote>)JSONArray.toCollection(jsonArray, DnOrderDeliveryNote.class);
JsonResult<String> jr = new JsonResult<String>();
try {
balanceAccountService.batchPassAcount(list);//serveic业务方法
} catch (Exception e) {
e.printStackTrace();
}
return jr;
}
总结:在jsp页面通过JSON.stringify()将对象转为json字符串;在controller利用@RequestBody得到json字符串 参数使用JSONArray接收;通过toCollectionq强转为所需的list集合