请求参数绑定

一、案例01

1.一、请求:

<a href="param/test1?username=刘阳&age=18">测试</a>

1.二、处理方法:

    @RequestMapping("/param/test1")
    public String testParam1(String username,int age) {
        System.out.println("testParam1");
        System.out.println(username);
        System.out.println(age);
        return "success";
    }

1.三、总结:

1.3.一、若请求参数名和处理方法的参数名能对应上则会自动绑定请求参数java

1.3.二、请求参数中参数的顺序和处理方法中参数的顺序不用保持一致spring

二、案例02

2.一、请求:

<a href="param/test1?username=刘阳">测试</a>

 

2.二、处理方法:

    @RequestMapping("/param/test1")
    public String testParam1(String username,int age) {
        System.out.println("testParam1");
        System.out.println(username);
        System.out.println(age);
        return "success";
    }

2.三、总结:

2.3.一、请求参数中没有参数age,用int类型去接收时会报错,由于null没法赋值给int类型,需用Integer类型接收mvc

三、案例03

3.一、请求:

<a href="param/test1?username=刘阳&age=20">测试</a>

 

3.二、处理方法:

    @RequestMapping("/param/test1")
    public String testParam2(String name,Integer ag) {
        System.out.println("testParam2");
        System.out.println(name);
        System.out.println(ag);
        return "success";
    }

3.三、总结:

3.3.一、若请求参数中的参数名和处理方法中的方法名不一致,则处理方法中的参数值都为nullapp

3.3.二、使用@RequestParam注解可解决请求参数名和处理方法中参数名不一致时不能赋值问题dom

    @RequestMapping("/param/test1")
    public String testParam2(@RequestParam("username") String name, @RequestParam("age") Integer ag) {
        System.out.println("testParam2");
        System.out.println(name);
        System.out.println(ag);
        return "success";
    }

四、案例04

4.一、请求:

<form action="param/test1" method="get">
    accountId:<input type="text" name="accountId"><br>
    accountName:<input type="text" name="accountName"><br>
    userId:<input type="text" name="user.userId"><br>
    userName:<input type="text" name="user.userName"><br>
    <input type="submit" value="提交">
</form>

4.二、处理方法:

    @RequestMapping("/param/test1")
    public String testParam3(Account act) {
        System.out.println("testParam3");
        System.out.println(act);
        return "success";
    }

4.三、实体类:

package com.ly.springmvc.domain;

import java.io.Serializable;

public class User implements Serializable {
    private Integer userId;
    private String userName;

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                '}';
    }
}
package com.ly.springmvc.domain;

import java.io.Serializable;

public class Account implements Serializable {
    private Integer accountId;
    private String accountName;
    private User user;

    public void setAccountId(Integer accountId) {
        this.accountId = accountId;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }

    @Override
    public String toString() {
        return "Account{" +
                "accountId=" + accountId +
                ", accountName='" + accountName + '\'' +
                ", user=" + user +
                '}';
    }
}

4.四、总结

4.4.一、实体类的属性必需要有对应的set方法才能够被自动赋值ide

4.4.二、name="user.userId"是为Account对象的user属性的userId属性赋值,前提条件是User对象中的属性必需要有set方法且Account中必需要有user属性的get方法post

五、案例05

5.一、请求:

<form action="param/test1" method="post">
    userId:<input type="text" name="userId"><br>
    userName:<input type="text" name="userName"><br>
    <h3>请求参数绑定集合类型</h3>
    accountId:<input type="text" name="accounts[0].accountId"><br>
    accountName:<input type="text" name="accounts[0].accountName"><br>
    accountId:<input type="text" name="accounts[1].accountId"><br>
    accountName:<input type="text" name="accounts[1].accountName"><br>
    <h3>请求参数绑定Map类型</h3>
    accountId:<input type="text" name="accountMap['key1'].accountId"><br>
    accountName:<input type="text" name="accountMap['key1'].accountName"><br>
    accountId:<input type="text" name="accountMap['key2'].accountId"><br>
    accountName:<input type="text" name="accountMap['key2'].accountName"><br>
    <input type="submit" value="提交">
</form>

5.二、处理方法:

    @RequestMapping("/param/test1")
    public String testParam5(User u) {
        System.out.println("testParam5");
        System.out.println(u);
        return "success";
    }

 

5.三、实体类

package com.ly.springmvc.domain;

import java.io.Serializable;

public class Account implements Serializable {
    private Integer accountId;
    private String accountName;

    public void setAccountId(Integer accountId) {
        this.accountId = accountId;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    @Override
    public String toString() {
        return "Account{" +
                "accountId=" + accountId +
                ", accountName='" + accountName + '\'' +
                '}';
    }
}
package com.ly.springmvc.domain;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class User implements Serializable {
    private Integer userId;
    private String userName;
    private List<Account> accounts;
    private Map<String,Account> accountMap;

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

    public void setAccountMap(Map<String, Account> accountMap) {
        this.accountMap = accountMap;
    }

    public List<Account> getAccounts() {
        return accounts;
    }

    public Map<String, Account> getAccountMap() {
        return accountMap;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", accounts=" + accounts +
                ", accountMap=" + accountMap +
                '}';
    }
}

5.四、总结:

5.4.一、Account和User的每一个属性都要有set方法测试

5.4.二、User类的accounts和accountMap属性都要有get方法this

5.4.三、name="accounts[0].accountId"是为User对象的accounts属性的第一个元素的accountId属性赋值spa

5.4.四、name="accountMap['key1'].accountName"是在User对象的accountMap属性中放入一个key值为key1的Account对象,为该Account对象的accountName属性赋值

相关文章
相关标签/搜索