[Spring MVC] - SpringMVC的各類參數綁定方式

SpringMVC的各類參數綁定方式spring

 

1. 基本數據類型(以int爲例,其餘相似):
Controller代碼:app

@RequestMapping("saysth.do")
public void test(int count) {
}

表單代碼:post

<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>

表單中input的name值和Controller的參數變量名保持一致,就能完成數據綁定,若是不一致可使用@RequestParam註解。須要注意的是,若是Controller方法參數中定義的是基本數據類型,可是從頁面提交過來的數據爲null或者」"的話,會出現數據轉換的異常。也就是必須保證表單傳遞過來的數據不能爲null或」",因此,在開發過程當中,對可能爲空的數據,最好將參數數據類型定義成包裝類型,具體參見下面的例子。this

 

2. 包裝類型(以Integer爲例,其餘相似):
Controller代碼:spa

@RequestMapping("saysth.do")
public void test(Integer count) {
}

表單代碼:code

<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>

和基本數據類型基本同樣,不一樣之處在於,表單傳遞過來的數據能夠爲null或」",以上面代碼爲例,若是表單中num爲」"或者表單中無num這個input,那麼,Controller方法參數中的num值則爲null。orm

 

3. 自定義對象類型:
Model代碼:對象

public class User {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

Controller代碼:blog

@RequestMapping("saysth.do")
public void test(User user) {
}

表單代碼:開發

<form action="saysth.do" method="post">
<input name="firstName" value="張" type="text"/>
<input name="lastName" value="三" type="text"/>
......
</form>

很是簡單,只需將對象的屬性名和input的name值一一匹配便可。

 

4. 自定義複合對象類型:
Model代碼:

public class ContactInfo {
    private String tel;
    private String address;

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

public class User {
    private String firstName;
    private String lastName;
    private ContactInfo contactInfo;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public ContactInfo getContactInfo() {
        return contactInfo;
    }

    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }

}

Controller代碼:

@RequestMapping("saysth.do")
public void test(User user) {
    System.out.println(user.getFirstName());
    System.out.println(user.getLastName());
    System.out.println(user.getContactInfo().getTel());
    System.out.println(user.getContactInfo().getAddress());
}

表單代碼:

<form action="saysth.do" method="post">
<input name="firstName" value="張" /><br>
<input name="lastName" value="三" /><br>
<input name="contactInfo.tel" value="13809908909" /><br>
<input name="contactInfo.address" value="北京海淀" /><br>
<input type="submit" value="Save" />
</form>

User對象中有ContactInfo屬性,Controller中的代碼和第3點說的一致,可是,在表單代碼中,須要使用「屬性名(對象類型的屬性).屬性名」來命名input的name。

 

5. List綁定:
List須要綁定在對象上,而不能直接寫在Controller方法的參數中。
Model代碼:

public class User {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

public class UserListForm {
    private List<User> users;

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

}

Controller代碼:

@RequestMapping("saysth.do")
public void test(UserListForm userForm) {
    for (User user : userForm.getUsers()) {
        System.out.println(user.getFirstName() + " - " + user.getLastName());
    }
}

表單代碼:

<form action="saysth.do" method="post">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="2"><input type="submit" value="Save" /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><input name="users[0].firstName" value="aaa" /></td>
<td><input name="users[0].lastName" value="bbb" /></td>
</tr>
<tr>
<td><input name="users[1].firstName" value="ccc" /></td>
<td><input name="users[1].lastName" value="ddd" /></td>
</tr>
<tr>
<td><input name="users[2].firstName" value="eee" /></td>
<td><input name="users[2].lastName" value="fff" /></td>
</tr>
</tbody>
</table>
</form>

其實,這和第4點User對象中的contantInfo數據的綁定有點相似,可是這裏的UserListForm對象裏面的屬性被定義成List,而不是普通自定義對象。因此,在表單中須要指定List的下標。值得一提的是,Spring會建立一個以最大下標值爲size的List對象,因此,若是表單中有動態添加行、刪除行的狀況,就須要特別注意,譬如一個表格,用戶在使用過程當中通過屢次刪除行、增長行的操做以後,下標值就會與實際大小不一致,這時候,List中的對象,只有在表單中對應有下標的那些纔會有值,不然會爲null,看個例子:

表單代碼:

<form action="saysth.do" method="post">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="2"><input type="submit" value="Save" /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><input name="users[0].firstName" value="aaa" /></td>
<td><input name="users[0].lastName" value="bbb" /></td>
</tr>
<tr>
<td><input name="users[1].firstName" value="ccc" /></td>
<td><input name="users[1].lastName" value="ddd" /></td>
</tr>
<tr>
<td><input name="users[20].firstName" value="eee" /></td>
<td><input name="users[20].lastName" value="fff" /></td>
</tr>
</tbody>
</table>
</form>

這個時候,Controller中的userForm.getUsers()獲取到List的size爲21,並且這21個User對象都不會爲null,可是,第2到第19的User對象中的firstName和lastName都爲null。打印結果:

aaa - bbb
ccc - ddd
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
eee - fff

 

6. Set綁定:
Set和List相似,也須要綁定在對象上,而不能直接寫在Controller方法的參數中。可是,綁定Set數據時,必須先在Set對象中add相應的數量的模型對象。
Model代碼:

public class User {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

public class UserSetForm {
    private Set<User> users = new HashSet<User>();

    public UserSetForm() {
        users.add(new User());
        users.add(new User());
        users.add(new User());
    }

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

}

Controller代碼:

@RequestMapping("saysth.do")
public void test(UserSetForm userForm) {
    for (User user : userForm.getUsers()) {
        System.out.println(user.getFirstName() + " - " + user.getLastName());
    }
}

表單代碼:

<form action="saysth.do" method="post">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="2"><input type="submit" value="Save" /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><input name="users[0].firstName" value="aaa" /></td>
<td><input name="users[0].lastName" value="bbb" /></td>
</tr>
<tr>
<td><input name="users[1].firstName" value="ccc" /></td>
<td><input name="users[1].lastName" value="ddd" /></td>
</tr>
<tr>
<td><input name="users[2].firstName" value="eee" /></td>
<td><input name="users[2].lastName" value="fff" /></td>
</tr>
</tbody>
</table>
</form>

基本和List綁定相似。
須要特別提醒的是,若是最大下標值大於Set的size,則會拋出org.springframework.beans.InvalidPropertyException異常。因此,在使用時有些不便。

 

7. Map綁定:
Map最爲靈活,它也須要綁定在對象上,而不能直接寫在Controller方法的參數中。
Model代碼:

public class User {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

public class UserMapForm {
    private Map<String, User> users;

    public Map<String, User> getUsers() {
        return users;
    }

    public void setUsers(Map<String, User> users) {
        this.users = users;
    }

}

Controller代碼:

@RequestMapping("saysth.do")
public void test(UserMapForm userForm) {
    for (Map.Entry<String, User> entry : userForm.getUsers().entrySet()) {
        System.out.println(entry.getKey() + ": " + entry.getValue().getFirstName() + " - " +
        entry.getValue().getLastName());
    }
}

表單代碼:

<form action="saysth.do" method="post">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="2"><input type="submit" value="Save" /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><input name="users['x'].firstName" value="aaa" /></td>
<td><input name="users['x'].lastName" value="bbb" /></td>
</tr>
<tr>
<td><input name="users['y'].firstName" value="ccc" /></td>
<td><input name="users['y'].lastName" value="ddd" /></td>
</tr>
<tr>
<td><input name="users['z'].firstName" value="eee" /></td>
<td><input name="users['z'].lastName" value="fff" /></td>
</tr>
</tbody>
</table>
</form>

打印結果:

x: aaa - bbb
y: ccc - ddd
z: eee - fff
相關文章
相關標籤/搜索