<a href="param/test1?username=劉陽&age=18">測試</a>
@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.一、若請求參數名和處理方法的參數名能對應上則會自動綁定請求參數java
1.3.二、請求參數中參數的順序和處理方法中參數的順序不用保持一致spring
<a href="param/test1?username=劉陽">測試</a>
@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.一、請求參數中沒有參數age,用int類型去接收時會報錯,由於null沒法賦值給int類型,需用Integer類型接收mvc
<a href="param/test1?username=劉陽&age=20">測試</a>
@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.一、若請求參數中的參數名和處理方法中的方法名不一致,則處理方法中的參數值都爲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"; }
<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>
@RequestMapping("/param/test1") public String testParam3(Account act) { System.out.println("testParam3"); System.out.println(act); return "success"; }
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.一、實體類的屬性必需要有對應的set方法才能夠被自動賦值ide
4.4.二、name="user.userId"是爲Account對象的user屬性的userId屬性賦值,前提條件是User對象中的屬性必需要有set方法且Account中必需要有user屬性的get方法post
<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>
@RequestMapping("/param/test1") public String testParam5(User u) { System.out.println("testParam5"); System.out.println(u); return "success"; }
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.一、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屬性賦值