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

Source: Internet
Author: User


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

In the SpringMVC environment, @RequestBody receives a string of Json objects instead of a Json object.
However, in ajax requests, Json objects are often passed, and JSON.stringify(data) can be used to convert objects into strings.
At the same time, the ajax request should also specify the dataType: "json", contentType: "application/json" This can easily transfer an object or List to the Java side!
Content-type

  Why does the server do special processing for form submission and file uploading, because the form submission data is a name-value pair, and the Content-Type is application/x-www-form-urlencoded,
The file upload server needs special processing. The normal post request (Content-Type is not application/x-www-form-urlencoded) is not fixed. The format is not necessarily the name-value pair.
Therefore, the server cannot know the specific processing method, so it can only be parsed by obtaining the original data stream. When jquery executes a post request, it sets the Content-Type to application/x-www-form-urlencoded, so the server can parse correctly, and when using the native ajax request,
If the Content-Type is not displayed, the default is text/plain. At this time, the server does not know how to parse the data, so the request data can only be parsed by obtaining the original data stream.
The first way to pass the parameter bean
Jsp file:
$("#saveuddd").click(function(){
                     Var saveDataAry=[];
                     Var data1={"name":"test","password":"gz"};
                     Var data2={"name":"ququ","password":"gr"};
                     saveDataAry.push(data1);
                     saveDataAry.push(data2);
                     $.ajax({
                         Type: "POST",
                         Url: basePath + "/user/saveUser",
                         dataType: "json",
                         contentType: "application/json", // Specifying this protocol is important
                         Data:JSON.stringify(saveDataAry), //This parameter is only available in json format, the background is parsed as an entity, and the background can be used directly.
                         Success:function(data){
                         }
                     });
                 }) 
Java file
  @RequestMapping(value = "saveUser", method = {RequestMethod.POST })
      @ResponseBody
     Public void saveu( @RequestBody List<User> users ){ //The user at this time is the map data, and it is not parsed as a bean // not recommended
          System.out.println(users) ;
     }
Use the following methods to use the bean
  @RequestMapping(value = "saveUser", method = {RequestMethod.POST })
      @ResponseBody
     Public void saveu( @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 as above
another kind:
  @RequestMapping(value = "saveUser", method = {RequestMethod.POST })
  @ResponseBody
  Public void saveu( @RequestBody List<Map<String,String>> users ){ // spring MVC can only parse the outer json format, the internal bean is converted to a map-type key-value pair, and the map needs to be parsed.
       List<User> userList = new ArrayList<User>();
       For(Map<String,String> map : users){
           User u = new User();
           u.setName(map.get("name"));
           u.setPassword(map.get("password"));
           userList.add(u);
       }
      // Here you can use userList
  } 




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 file
$("#saveuddd").click(function(){
                    Var saveDataAry=[];
                    Var data1={"name":"test","password":"gz"};
                    Var data2={"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"},
                          // can pass multiple parameters, but the user content at this time is passed along with the content of another parameter: aa, so the background is parsed into a string
                         // The contents of the users received in the background: [{"name":"test","password":"gz"},{"name":"ququ","password":"gr"}]
                         // aa content received in the background: "ddd"
                        Success:function(data){

                        }
                    });
                }) 

Java file
@RequestMapping(value = "saveUser", method = {RequestMethod.POST })
      @ResponseBody
     Public void 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

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.