測試開發進階——spring boot——MVC——get訪問——接收前端傳遞的陣列
阿新 • • 發佈:2021-07-31
控制器:
package com.awaimai.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RestController public class kzq { @RequestMapping("/param/requestarray") @ResponseBody public Map<String, Object> requestArray(String[] names, int[] ages, double[] scores) { Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("name", names); paramMap.put("age", ages); paramMap.put("score", scores); return paramMap; } }
web訪問:
springMVC中,前端往後端除了可以傳遞一些單值外,也可以傳遞陣列。
傳遞資料部分沒有太多規則可言,在後臺控制器部分,定義陣列形式接收資料,前端傳遞時,將一組資料用英文的逗號“,”隔開就可以了。