SoFunction
Updated on 2025-03-08

Foreground pass list array to receive array sample code by backend spring MVC

Preface

This article mainly introduces to you the relevant content about the front-end transfer list array received by the back-end spring MVC. It is shared for your reference and learning. I won’t say much below, let’s take a look at the detailed introduction together.

In development, sometimes you need to customize objects in the foreground, then encapsulate the objects in a list, and transmit them to the background. This idea is also more reasonable. Let’s take a look at the example code directly:

1. Foreground code

$ = function () {

 $ = new Array();//Custom array
  ($, function (record, index) {

   

  if ( != null) {

   $ = {'userAnswerId': null,'score': null};//Custom object structure
   $ = ;//Assignment
   $ = ;

    

   $($);//Encapsulate objects in collection
   debugger;

  }

 });

  

 if ($ != null && $ > 0) {

  var fd = new FormData();// Use angularJS's FormData to encapsulate the data to be transmitted
  var userScoreRecords = ($);//Convert object (set) to json string
  ('userScoreRecords', userScoreRecords);//Put the parameters into formData
  debugger;//Use debugger mode to view the value transmission situation
  $('/reviewProcess/save', fd, { //Use the post method to transfer formdata object
   transformRequest: , //Use angular authentication
   headers: {

    'Content-Type': undefined //Set request header
   }

  })

  .success(function (data){

   ("success");

  })

  .error(function (data) {

   ("failed");

  });

 }

}; 

2. Backend reception

@ResponseBody

 @RequestMapping(value = "/reviewProcess/save", method = )

 public void saveUserScore (@RequestParam("userScoreRecords") String userScoreRecords) { // Use requestparam to receive the json string transmitted by the front desk
  (userScoreRecords);

  ObjectMapper mapper = new ObjectMapper(); // Deserialize json string as object using fastJson's ObjectMapper
  UserScoreModel record = null;

  try {

   JSONArray jsonArray = new JSONArray (userScoreRecords); //Convert json string to json array in the background
   for (int i =0; i < (); i++) {

    record = ((i).toString(), ); //Get the json object of the json array and deserialize it into the corresponding object
    (record); // After obtaining the object, you can operate it
   }

  } catch (Exception e) {

   ((), e);

  }

 } 

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.