本文主要是介绍spring-mvc中controller,前端传过来的参数,用数组,列表,Map来接受参数的方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
spring-mvc中controller,前端传过来的参数,用数组,列表,Map来接受参数的方式。
仅使用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;
-
}
-
}
-
-
用List接受参数
-
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;
-
}
-
}
-
-
用Map接受参数
-
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
里面的参数信息的。-
import lombok.Data;
-
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;
-
@Data
-
class Spouse{
-
private String name;
-
private String age;
-
private String sex;
-
}
-
@Controller
-
public class CIsBestController {
-
// 访问路径 http://localhost:8080/map?name=貂蝉&age=18&sex=女&spouse.name=吕布&spouse.age=25&spouse.sex=男
-
@RequestMapping("/map")
-
@ResponseBody
-
public String resp(@RequestParam Map map) {
-
System.out.println(map);
-
String name = (String) map.get("name");
-
System.out.println(name);
-
Spouse spouse = (Spouse) map.get("spouse");
-
System.out.println(spouse.getName());
-
return "响应数据:" + spouse.toString();
-
}
-
}
访问地址
http://localhost:8080/map?name=貂蝉&age=18&sex=女&spouse.name=吕布&spouse.age=25&spouse.sex=男
,会报java.lang.NullPointerException
的异常。 -
这篇关于spring-mvc中controller,前端传过来的参数,用数组,列表,Map来接受参数的方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!