国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

SpringMVC 數據綁定實例詳解

2024-09-05 00:23:01
字體:
來源:轉載
供稿:網友

 SpringMVC 數據綁定

查看spring源碼可以看出spring支持轉換的數據類型:

org.springframework.beans.PropertyEditorRegistrySupport:

/**  * Actually register the default editors for this registry instance.  */ private void createDefaultEditors() {   this.defaultEditors = new HashMap<Class, PropertyEditor>(64);    // Simple editors, without parameterization capabilities.   // The JDK does not contain a default editor for any of these target types.   this.defaultEditors.put(Charset.class, new CharsetEditor());   this.defaultEditors.put(Class.class, new ClassEditor());   this.defaultEditors.put(Class[].class, new ClassArrayEditor());   this.defaultEditors.put(Currency.class, new CurrencyEditor());   this.defaultEditors.put(File.class, new FileEditor());   this.defaultEditors.put(InputStream.class, new InputStreamEditor());   this.defaultEditors.put(InputSource.class, new InputSourceEditor());   this.defaultEditors.put(Locale.class, new LocaleEditor());   this.defaultEditors.put(Pattern.class, new PatternEditor());   this.defaultEditors.put(Properties.class, new PropertiesEditor());   this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());   this.defaultEditors.put(TimeZone.class, new TimeZoneEditor());   this.defaultEditors.put(URI.class, new URIEditor());   this.defaultEditors.put(URL.class, new URLEditor());   this.defaultEditors.put(UUID.class, new UUIDEditor());    // Default instances of collection editors.   // Can be overridden by registering custom instances of those as custom editors.   this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));   this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));   this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));   this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));   this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));    // Default editors for primitive arrays.   this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());   this.defaultEditors.put(char[].class, new CharArrayPropertyEditor());    // The JDK does not contain a default editor for char!   this.defaultEditors.put(char.class, new CharacterEditor(false));   this.defaultEditors.put(Character.class, new CharacterEditor(true));    // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.   this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));   this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));    // The JDK does not contain default editors for number wrapper types!   // Override JDK primitive number editors with our own CustomNumberEditor.   this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));   this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));   this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));   this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));   this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));   this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));   this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false));   this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true));   this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false));   this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));   this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false));   this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true));   this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));   this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));    // Only register config value editors if explicitly requested.   if (this.configValueEditorsActive) {     StringArrayPropertyEditor sae = new StringArrayPropertyEditor();     this.defaultEditors.put(String[].class, sae);     this.defaultEditors.put(short[].class, sae);     this.defaultEditors.put(int[].class, sae);     this.defaultEditors.put(long[].class, sae);   } } 

下面挑選一些常用的數據類型,舉例說明它們的綁定方式

1. 基本數據類型(以int為例,其他類似):

    Controller代碼:

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

    JSP表單代碼:

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

表單中input的name值和Controller的參數變量名保持一致,就能完成基本數據類型的數據綁定,如果不一致可以使用@RequestParam標注實現。值得一提的是,如果Controller方法參數中定義的是基本數據類型,但是從jsp提交過來的數據為null或者""的話,會出現數據轉換的異常。也就是說,必須保證表單傳遞過來的數據不能為null或"",所以,在開發過程中,對可能為空的數據,最好將參數數據類型定義成包裝類型,具體參見下面的第二條。

2. 包裝類型(以Integer為例,其他類似):

    Controller代碼:

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

   JSP表單代碼:

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

和基本數據類型基本一樣,不同之處在于,JSP表單傳遞過來的數據可以為null或"",以上面代碼為例,如果jsp中num為""或者表單中無num這個input,那么,Controller方法參數中的num值則為null。

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代碼:

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

    JSP表單代碼:

<form action="test.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("test.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()); } 

    JSP表單代碼:

<form action="test.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點說的一致,但是,在jsp代碼中,需要使用“屬性名(對象類型的屬性).屬性名”來命名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("test.do") public void test(UserListForm userForm) {   for (User user : userForm.getUsers()) {     System.out.println(user.getFirstName() + " - " + user.getLastName());   } } 

    JSP表單代碼:

<form action="test.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,而不是普通自定義對象。所以,在JSP中需要指定List的下標。值得一提的是,Spring會創建一個以最大下標值為size的List對象,所以,如果JSP表單中有動態添加行、刪除行的情況,就需要特別注意,譬如一個表格,用戶在使用過程中經過多次刪除行、增加行的操作之后,下標值就會與實際大小不一致,這時候,List中的對象,只有在jsp表單中對應有下標的那些才會有值,否則會為null,看個例子:

    JSP表單代碼:

<form action="test.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("test.do") public void test(UserSetForm userForm) {   for (User user : userForm.getUsers()) {     System.out.println(user.getFirstName() + " - " + user.getLastName());   } } 

    JSP表單代碼:

<form action="test.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異常。所以,在使用時有些不便。暫時沒找到解決方法,如果有網友知道,請回帖共享你的做法。

5. 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("test.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());   } } 

    JSP表單代碼:

<form action="test.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 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到JSP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐昌市| 新和县| 姚安县| 同德县| 河池市| 天全县| 怀宁县| 喀什市| 辽宁省| 都匀市| 封丘县| 天气| 平南县| 安陆市| 渝中区| 宁晋县| 吕梁市| 肥城市| 高青县| 铁岭县| 抚松县| 祁东县| 仙桃市| 云阳县| 乌鲁木齐市| 海淀区| 资源县| 清镇市| 象山县| 盈江县| 定陶县| 三江| 阿拉善右旗| 安宁市| 河东区| 耒阳市| 勐海县| 崇明县| 兴山县| 容城县| 临安市|