标签:script string strong += body let pre org 列表
仅使用get方法来进行演示,其他请求方法(POST,DELETE,PUT)接受参数的形式都是一样的。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 访问路径 http://localhost:8080/array?strings="貂蝉学python","小乔学java","鲁班学C++"
// 浏览器显示的结果:响应数据:"貂蝉学python","小乔学java","鲁班学C ",
@RequestMapping("/array")
@ResponseBody
public String resp(String[] strings) {
String str = "";
for(String s:strings){
System.out.println("接受的参数是:" + s);
str += s+",";
}
return "响应数据:" + str;
}
}
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
@Controller
public class CIsBestController {
// 访问路径 http://localhost:8080/list?array_list="貂蝉学python","小乔学java","鲁班学C++"
// 浏览器显示的结果:响应数据:"貂蝉学python","小乔学java","鲁班学C ",
@RequestMapping("/list")
@ResponseBody
public String resp(@RequestParam("array_list") ArrayList<String> arrayList) {
String str = "";
for(String s:arrayList){
System.out.println("接受的参数是:" + s);
str += s+",";
}
return "响应数据:" + str;
}
}
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
@Controller
public class CIsBestController {
// 访问路径 http://localhost:8080/map?name=貂蝉&age=18&sex=女
// 浏览器显示的结果:响应数据:{name=貂蝉, age=18, sex=女}
@RequestMapping("/map")
@ResponseBody
public String resp(@RequestParam Map map) {
System.out.println(map);
return "响应数据:" + map;
}
}
关于用Map接受参数有个小坑,用Map接受参数必须要在参数前添加 @RequestParam
注解。
Map不能用来接受复合参数。对于前端来说,Map只能接受
{
name : "貂蝉",
age : 18,
sex : "女"
}
这种形式的参数。如果前端传的是
{
name : "貂蝉",
age : 18,
sex : "女",
spouse : {
name : "吕布",
age: 25,
sex: "男"
}
}
Map 是拿不到 spouse
里面的参数信息的。
spring-mvc中controller,前端传过来的参数,用数组,列表,Map来接受参数的方式
标签:script string strong += body let pre org 列表
原文地址:https://www.cnblogs.com/cisbest/p/13511738.html