主頁 > 知識庫 > SpringMVC 數(shù)據(jù)綁定實例詳解

SpringMVC 數(shù)據(jù)綁定實例詳解

熱門標(biāo)簽:泰州泰興400電話 怎么申請 如何用中國地圖標(biāo)注數(shù)字點 地圖標(biāo)注市場怎么樣 企業(yè)怎么在聯(lián)通申請400電話 百度地圖添加標(biāo)注圖標(biāo)樣式 好操作的電話機器人廠家 南昌市地圖標(biāo)注app 南京新思維電話機器人 聊城智能電銷機器人外呼

 SpringMVC 數(shù)據(jù)綁定

查看spring源碼可以看出spring支持轉(zhuǎn)換的數(shù)據(jù)類型:

org.springframework.beans.PropertyEditorRegistrySupport:

/** 
 * Actually register the default editors for this registry instance. 
 */ 
private void createDefaultEditors() { 
  this.defaultEditors = new HashMapClass, 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); 
  } 
} 

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

1. 基本數(shù)據(jù)類型(以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的參數(shù)變量名保持一致,就能完成基本數(shù)據(jù)類型的數(shù)據(jù)綁定,如果不一致可以使用@RequestParam標(biāo)注實現(xiàn)。值得一提的是,如果Controller方法參數(shù)中定義的是基本數(shù)據(jù)類型,但是從jsp提交過來的數(shù)據(jù)為null或者""的話,會出現(xiàn)數(shù)據(jù)轉(zhuǎn)換的異常。也就是說,必須保證表單傳遞過來的數(shù)據(jù)不能為null或"",所以,在開發(fā)過程中,對可能為空的數(shù)據(jù),最好將參數(shù)數(shù)據(jù)類型定義成包裝類型,具體參見下面的第二條。

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> 

和基本數(shù)據(jù)類型基本一樣,不同之處在于,JSP表單傳遞過來的數(shù)據(jù)可以為null或"",以上面代碼為例,如果jsp中num為""或者表單中無num這個input,那么,Controller方法參數(shù)中的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值一一對應(yīng)即可。

4. 自定義復(fù)合對象類型:

    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方法的參數(shù)中。

    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 ListUser> users; 
 
  public ListUser> getUsers() { 
    return users; 
  } 
 
  public void setUsers(ListUser> 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數(shù)據(jù)的綁定有點類似,但是這里的UserListForm對象里面的屬性被定義成List,而不是普通自定義對象。所以,在JSP中需要指定List的下標(biāo)。值得一提的是,Spring會創(chuàng)建一個以最大下標(biāo)值為size的List對象,所以,如果JSP表單中有動態(tài)添加行、刪除行的情況,就需要特別注意,譬如一個表格,用戶在使用過程中經(jīng)過多次刪除行、增加行的操作之后,下標(biāo)值就會與實際大小不一致,這時候,List中的對象,只有在jsp表單中對應(yīng)有下標(biāo)的那些才會有值,否則會為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。打印結(jié)果:

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方法的參數(shù)中。但是,綁定Set數(shù)據(jù)時,必須先在Set對象中add相應(yīng)的數(shù)量的模型對象。

    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 SetUser> users = new HashSetUser>(); 
   
  public UserSetForm(){ 
    users.add(new User()); 
    users.add(new User()); 
    users.add(new User()); 
  } 
 
  public SetUser> getUsers() { 
    return users; 
  } 
 
  public void setUsers(SetUser> 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綁定類似。

需要特別提醒的是,如果最大下標(biāo)值大于Set的size,則會拋出org.springframework.beans.InvalidPropertyException異常。所以,在使用時有些不便。暫時沒找到解決方法,如果有網(wǎng)友知道,請回帖共享你的做法。

5. Map綁定:

    Map最為靈活,它也需要綁定在對象上,而不能直接寫在Controller方法的參數(shù)中。

    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 MapString, User> users; 
 
  public MapString, User> getUsers() { 
    return users; 
  } 
 
  public void setUsers(MapString, User> users) { 
    this.users = users; 
  } 
 
} 

    Controller代碼:

@RequestMapping("test.do") 
public void test(UserMapForm userForm) { 
  for (Map.EntryString, 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> 

打印結(jié)果:

x: aaa - bbb 
y: ccc - ddd 
z: eee - fff 

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

您可能感興趣的文章:
  • SpringMVC實現(xiàn)數(shù)據(jù)綁定及表單標(biāo)簽
  • SpringMVC 參數(shù)綁定意義及實現(xiàn)過程解析
  • SpringMVC中使用@PathVariable綁定路由中的數(shù)組的方法
  • SpringMVC中利用@InitBinder來對頁面數(shù)據(jù)進(jìn)行解析綁定的方法
  • 一篇文章帶你了解SpringMVC數(shù)據(jù)綁定

標(biāo)簽:白銀 烏蘭察布 臨汾 山南 銅川 開封 自貢 吉林

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SpringMVC 數(shù)據(jù)綁定實例詳解》,本文關(guān)鍵詞  SpringMVC,數(shù)據(jù),綁定,實例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SpringMVC 數(shù)據(jù)綁定實例詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于SpringMVC 數(shù)據(jù)綁定實例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章