springmvc 5种接受参数类型
1.普通参数
当我们的请求参数和我们的接口(方法)参数名称不相同时,就可以使用@RequestParam(name="userName",required = false)注解绑定参数
name : 绑定的参数名称,请求传递的参数名
required : true表示请求传递的参数不能为空(null)false表示请求传递的参数可以为空(null)
package com.painter.controller;
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;
/**
* @Author: Painter
* @project_name: spring_mvc
* @system_login: sunshine
* @time: 2022/10/1016:29
*/
@Controller
@RequestMapping("/param")
public class MyController {
/**
* 普通参数
* @param name 访问路径传递的参数值(但是与我们的请求参数名不想同所以需要使用@RequestParam注解绑定参数)
* @param paw
* @return
*
* * @RequestParam(name="userName",required = false) 参数绑定注解
* 表示绑定请求参数userName
* required = false 表示非必须参数(可以不传递),如果为true 表示必须参数(必须传递)
*/
@ResponseBody
@RequestMapping("/commonparameters")
public String commonParameters(@RequestParam(name="userName",required = false)String name,
@RequestParam(name="password",required = false)String paw){
return "username:"+name+" password:"+paw;
}
}
2.对象参数
请求参数名与形参对象属性名相同,定义对象类型形参即可接收参数
实体类的成员属性必须和请求传递的参数名相同
package com.painter.entity;
/**
* @Author: Painter
* @project_name: spring_mvc
* @system_login: sunshine
* @time: 2022/10/1016:56
*/
public class UserEntity {
String userName;
String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
控制类
/**
* 对象参数 先创建实体类
* @param userEntity UserEntity实体类的实例
* @return
*/
@ResponseBody
@RequestMapping("/objectparameters")
public String objectParameters(UserEntity userEntity){
System.out.println("username:"+userEntity.getUserName()+" password:"+userEntity.getPassword());
return "username:"+userEntity.getUserName()+" password:"+userEntity.getPassword();
}
3.嵌套对象参数
嵌套对象参数:请求参数名与形参对象属性名相同,按照对象层次结构关系即可接收嵌套对象属性参数
实体类
package com.painter.entity;
/**
* @Author: Painter
* @project_name: spring_mvc
* @system_login: sunshine
* @time: 2022/10/1016:56
*/
public class ObjUserEntity {
private String userName;
private String password;
private UserInfoEntity userInfoEntity;
public UserInfoEntity getUserInfoEntity() {
return userInfoEntity;
}
public void setUserInfoEntity(UserInfoEntity userInfoEntity) {
this.userInfoEntity = userInfoEntity;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
嵌套对象
package com.painter.entity;
import org.springframework.stereotype.Controller;
/**
* @Author: Painter
* @project_name: spring_mvc
* @system_login: sunshine
* @time: 2022/10/1018:14
*/
public class UserInfoEntity {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
接口(方法)
/**
* 对象参数 先创建实体类
* @param userEntity UserEntity实体类的实例
* @return
*/
@ResponseBody
@RequestMapping(value = "/objectsparameters",produces="text/html;charset=UTF-8")
public String objectsParameters(ObjUserEntity userEntity){
return "id:"+userEntity.getUserInfoEntity().getId()+",username:"+userEntity.getUserName()+", password:"+userEntity.getPassword();
}
访问地址
http://localhost:8080/param/objectsparameters?userName=painter&password=123123&userInfoEntity.id=001
这里的 serInfoEntity.id=001 为嵌套对象的属性id赋值
因为我们的请求传递的参数是对象参数 ,对象为ObjUserEntity 实体类,第三个参数
为 private UserInfoEntity userInfoEntity; 对象
4.数组参数
/**
* 数组类型参数
* http://localhost:8080/param/arraysparameters?id=001&id=002&id=003
* 同一个参数名多次赋值
* @param id 需要和请求传递的参数名相同
* @return
*/
@ResponseBody
@RequestMapping(value = "/arraysparameters",produces="text/html;charset=UTF-8")
public String arraysParameters(String[] id){
return Arrays.toString(id);
}
5.集合普通参数
/**
* 集合类型参数
* http://localhost:8080/param/listparameters?id=001&id=002&id=003
* 同一个参数名多次赋值
* @param id 需要和请求传递的参数名相同
* @return
*/
@ResponseBody
@RequestMapping(value = "/listparameters",produces="text/html;charset=UTF-8")
public String listParameters(@RequestParam List<String> id){
return Arrays.toString(id.toArray());
}