请求参数绑定
1、案例01
1.1、请求:
<a href="param/test1?username=刘阳&age=18">测试</a>
1.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"; }
1.3、总结:
1.3.1、若请求参数名和处理方法的参数名能对应上则会自动绑定请求参数
1.3.2、请求参数中参数的顺序和处理方法中参数的顺序不用保持一致
2、案例02
2.1、请求:
<a href="param/test1?username=刘阳">测试</a>
2.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.3、总结:
2.3.1、请求参数中没有参数age,用int类型去接收时会报错,因为null无法赋值给int类型,需用Integer类型接收
3、案例03
3.1、请求:
<a href="param/test1?username=刘阳&age=20">测试</a>
3.2、处理方法:
@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.3.1、若请求参数中的参数名和处理方法中的方法名不一致,则处理方法中的参数值都为null
3.3.2、使用@RequestParam注解可解决请求参数名和处理方法中参数名不一致时不能赋值问题
@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"; }
4、案例04
4.1、请求:
<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.2、处理方法:
@RequestMapping("/param/test1") public String testParam3(Account act) { System.out.println("testParam3"); System.out.println(act); return "success"; }
4.3、实体类:
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.4.1、实体类的属性必须要有对应的set方法才可以被自动赋值
4.4.2、name="user.userId"是为Account对象的user属性的userId属性赋值,前提条件是User对象中的属性必须要有set方法且Account中必须要有user属性的get方法
5、案例05
5.1、请求:
<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.2、处理方法:
@RequestMapping("/param/test1") public String testParam5(User u) { System.out.println("testParam5"); System.out.println(u); return "success"; }
5.3、实体类
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.4、总结:
5.4.1、Account和User的每个属性都要有set方法
5.4.2、User类的accounts和accountMap属性都要有get方法
5.4.3、name="accounts[0].accountId"是为User对象的accounts属性的第一个元素的accountId属性赋值
5.4.4、name="accountMap['key1'].accountName"是在User对象的accountMap属性中放入一个key值为key1的Account对象,为该Account对象的accountName属性赋值
相识是缘