spring MVC(六)

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());
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_painter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值