SPRINGMVC passing a parameter list object through Ajax or passing an array object to the background

Source: Internet
Author: User

Tags: Spring data Object Enc JSON why ASE file list

SPRINGMVC Pass the parameter list object through Ajax or pass an array object to the background environment:
    • Foreground pass parameters to background
    • The front desk uses Ajax
    • Backstage Use SPRINGMVC
    • The passed parameter is n multiple objects

JSON objects and JSON strings
在SpringMVC环境中,@RequestBody接收的是一个Json对象的字符串,而不是一个Json对象。
然而在ajax请求往往传的都是Json对象,用 JSON.stringify(data)的方式就能将对象变成字符串。
同时ajax请求的时候也要指定dataType: "json",contentType:"application/json" 这样就可以轻易的将一个对象或者List传到Java端!

Content-type
  服务器为什么会对表单提交和文件上传做特殊处理,因为表单提交数据是名值对的方式,且Content-Type为application/x-www-form-urlencoded,
而文件上传服务器需要特殊处理,普通的post请求(Content-Type不是application/x-www-form-urlencoded)数据格式不固定,不一定是名值对的方式,
所以服务器无法知道具体的处理方式,所以只能通过获取原始数据流的方式来进行解析。jquery在执行post请求时,会设置Content-Type为application/x-www-form-urlencoded,所以服务器能够正确解析,而使用原生ajax请求时,
如果不显示的设置Content-Type,那么默认是text/plain,这时服务器就不知道怎么解析数据了,所以才只能通过获取原始数据流的方式来进行解析请求数据。


The first way to pass the parameter bean
JSP files: $ ("#saveuddd"). Click (function(){                    varsavedataary=[]; vardata1={"name": "Test", "Password": "GZ"}; vardata2={"name": "Ququ", "Password": "GR"};                    Savedataary.push (DATA1);                    Savedataary.push (DATA2); $.ajax ({type:"POST", Url:basepath+ "/user/saveuser", DataType:"JSON", ContentType:"Application/json",//It 's important to specify this protocol .Data:JSON.stringify (Savedataary),//only this one parameter, JSON format, background resolution to the entity, the background can be directly usedSuccessfunction(data) {}}); })
Java file @RequestMapping (value= "Saveuser", method ={requestmethod.post}) @ResponseBody Public voidSaveu (@RequestBody list<user> users) {//the user at this time is the map data and is not parsed as a bean//do not recommend usingSystem.out.println (users); Use the following method to use the bean @RequestMapping (value= "Saveuser", method ={requestmethod.post}) @ResponseBody Public voidSaveu (@RequestBody user[] users) {System.out.println (Users.getname ());//the user at this time is the entity Bean}

The second way to pass the key-value pair
jsp above another: @RequestMapping (value= "Saveuser", method ={requestmethod.post}) @ResponseBody Public voidSaveu (@RequestBody list<map<string,string>> users) {//Spring MVC can only parse the outer JSON format, and the internal bean is converted to a key-value pair in the map format, which requires a map parsingList<user> userlist =NewArraylist<user>();  for(map<string,string>map:users) {User U=NewUser (); U.setname (Map.get ("Name")); U.setpassword (Map.get ("Password"));      Userlist.add (U); }     //we can use userlist here.}

The Third Way

Ability to pass multiple arguments, but each parameter is a string (the bean-type data is converted to a JSON-formatted string)

JSP files $ ("#saveuddd"). Click (function(){                    varsavedataary=[]; vardata1={"name": "Test", "Password": "GZ"}; vardata2={"name": "Ququ", "Password": "GR"};                    Savedataary.push (DATA1);                    Savedataary.push (DATA2); $.ajax ({type:"POST", Url:basepath+ "/user/saveuser", DataType:"JSON", Data:{users:json.stringify (savedataary), AA:"DDD"},                          //multiple parameters can be passed, but the users content at this time is passed with the contents of another parameter: AA, so the background resolves to a string                         //users content received in the background: [{"Name": "Test", "Password": "GZ"},{"name": "Ququ", "Password": "GR"}]                         //AA content received in the background: "DDD"Successfunction(data) {}}); })
= "Saveuser", method = {requestmethod.post})     @ResponseBody    publicvoid  Saveu (string users, String aa) {         System.out.println (users);    }

Reprinted from 73361104

 
 

Springmvc Pass the parameter list object through Ajax or pass an array object to the background

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: