SpringMVC框架的参数匹配

1. 基本数据类型(int为例)


//Controller代码

@RequestMapping("/hello")

public String mytest(int id){

      logger.info(id);

      return "demo"; 

}

输入请求:http://localhost:8080/Web/test/hello?id=100

打印出:

100

 

2.包装类型数据(Integer为例)

//Controller代码

@RequestMapping("/hello")

public String mytest(Integer id){

      logger.info(id);

      return "demo"; 

}

输入请求:http://localhost:8080/Web/test/hello?id=100

打印出:

100

 

3.自定义对象

//定义一个model

public class User {

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private int age;
    private String name;
    
}

3.1一个自定义对象

//Controller代码

@RequestMapping("/hello")

public String mytest(User user){

        logger.info("name is "+user.getName());

        logger.info("age is "+user.getAge());

        return "demo";
}

输入请求:http://localhost:8080/Web/test/hello?age=15&name=zhangsan

打印出:name is zhangsan

                age is 15

 

3.2自定义对象和基本数据类型一起

如果将自定义对象和基本数据类型的数据一起作为请求的参数,那么如下所示:

//Controller代码

@RequestMapping("/hello")

public String mytest(User user,int id){

        logger.info("name is "+user.getName());

        logger.info("age is "+user.getAge());

        logger.info("id is "+id);

        return "demo";
}

输入请求:http://localhost:8080/Web/test/hello?age=15&name=zhangsan&id=17

打印出:

name is zhangsan

age is 15

id is 17

3.3请求参数有重复的key

//Controller代码

@RequestMapping("/hello")
    public String mytest(String name,User user,int age ){
        logger.info("String name is "+name);
        logger.info("name is "+user.getName());
        logger.info("age is "+user.getAge());
        logger.info("int age is "+age);
        return "demo";
    }

输入请求:http://localhost:8080/Web/test/hello?name=zhanger&age=15&name=zhangsan&age=17&name=zhangsi

打印出:

String name is zhanger,zhangsan,zhangsi

name is zhanger,zhangsan,zhangsi

age is 15

int age is 15

从这个例子中可以看出,在controller中有重名的参数时,均被自定义的对象获取,而且String类型的参数获取多个参数,int类型的对象只获取第一个参数

 

//Controller代码

    @RequestMapping("/hello")
    public String mytest(int age,String name,User user1,User user2){
        logger.info("int age is "+age);
        logger.info("String name is "+name);
        logger.info("user1 name is "+user1.getName());
        logger.info("user1 age is "+user1.getAge());
        logger.info("user2 name is "+user2.getName());
        logger.info("user2 age is "+user2.getAge());
        logger.info(user1.equals(user2));
        return "demo";
    }

输入请求:http://localhost:8080/Web/test/hello?name=zhanger&age=15&name=zhangsan&age=17&name=zhangsi

打印出:

int age is 15

String name is zhanger,zhangsan,zhangsi

user1 name is zhanger,zhangsan,zhangsi

user1 age is 15

user2 name is zhanger,zhangsan,zhangsi

user2 age is 15

true

由这个例子可以看出,在controller中有多个自定义对象的时候,并不是按照对象中字段名出现的顺序来匹配第一个、第二个等对象的,而是作为一个对象处理,这些对象都是同一个对象。这个对象匹配的请求参数,String类型的请求参数是一个个加入到这个对象中的,int类型的对象是只匹配到重名的第一个请求参数。

4.对象嵌套

//定义model

public class Head {
    
    public int getLength() {
        return length;
    }
    public void setLength(int length) {
        this.length = length;
    }
    public String getEye() {
        return eye;
    }
    public void setEye(String eye) {
        this.eye = eye;
    }
    private int length;
    private String eye;

}

 

public class User {
    
    private int age;
    private String name;
    private Head head;
    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Head getHead() {
        return head;
    }
    public void setHead(Head head) {
        this.head = head;
    }
    
}

 

//Controller代码

    @RequestMapping("/hello")
    public String mytest(User user){
        logger.info("name is "+user.getName());
        logger.info("age is "+user.getAge());
        logger.info("eye is "+user.getHead().getEye());
        logger.info("length is "+user.getHead().getLength());
        return "demo";
    }

输入请求:http://localhost:8080/Web/test/hello?name=zhanger&age=15&head.length=10&head.eye=big

打印出:

name is zhanger

age is 15

eye is big

length is 10

使用User实体对象中的head.length等作为请求的参数,从而可以完成对象嵌套的参数匹配

5.List形式对象的匹配

//定义model

public class User {
    
    private int age;
    private String name;
    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

 

public class UserList {
    
    List<User> userList;

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
    
}
 

 

//Controller代码

    @RequestMapping("/hello")
    public String mytest(UserList list){
        for(User user:list.userList) {
            logger.info("user name is "+user.getName()+",age is "+user.getAge());
        }
        return "demo";
    }

 

输入请求:

待续。。。。。。。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值