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

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

熱門標(biāo)簽:服務(wù)器配置 銀行業(yè)務(wù) Mysql連接數(shù)設(shè)置 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Linux服務(wù)器 電子圍欄 團(tuán)購(gòu)網(wǎng)站 阿里云

 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ù)類型,舉例說(shuō)明它們的綁定方式

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)注實(shí)現(xiàn)。值得一提的是,如果Controller方法參數(shù)中定義的是基本數(shù)據(jù)類型,但是從jsp提交過(guò)來(lái)的數(shù)據(jù)為null或者""的話,會(huì)出現(xiàn)數(shù)據(jù)轉(zhuǎn)換的異常。也就是說(shuō),必須保證表單傳遞過(guò)來(lái)的數(shù)據(jù)不能為null或"",所以,在開發(fā)過(guò)程中,對(duì)可能為空的數(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表單傳遞過(guò)來(lái)的數(shù)據(jù)可以為null或"",以上面代碼為例,如果jsp中num為""或者表單中無(wú)num這個(gè)input,那么,Controller方法參數(shù)中的num值則為null。

3. 自定義對(duì)象類型:

    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> 

非常簡(jiǎn)單,只需將對(duì)象的屬性名和input的name值一一對(duì)應(yīng)即可。

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

    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對(duì)象中有ContactInfo屬性,Controller中的代碼和第3點(diǎn)說(shuō)的一致,但是,在jsp代碼中,需要使用“屬性名(對(duì)象類型的屬性).屬性名”來(lái)命名input的name。

5. List綁定:

    List需要綁定在對(duì)象上,而不能直接寫在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> 

其實(shí),這和第4點(diǎn)User對(duì)象中的contantInfo數(shù)據(jù)的綁定有點(diǎn)類似,但是這里的UserListForm對(duì)象里面的屬性被定義成List,而不是普通自定義對(duì)象。所以,在JSP中需要指定List的下標(biāo)。值得一提的是,Spring會(huì)創(chuàng)建一個(gè)以最大下標(biāo)值為size的List對(duì)象,所以,如果JSP表單中有動(dòng)態(tài)添加行、刪除行的情況,就需要特別注意,譬如一個(gè)表格,用戶在使用過(guò)程中經(jīng)過(guò)多次刪除行、增加行的操作之后,下標(biāo)值就會(huì)與實(shí)際大小不一致,這時(shí)候,List中的對(duì)象,只有在jsp表單中對(duì)應(yīng)有下標(biāo)的那些才會(huì)有值,否則會(huì)為null,看個(gè)例子:

    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> 

這個(gè)時(shí)候,Controller中的userForm.getUsers()獲取到List的size為21,而且這21個(gè)User對(duì)象都不會(huì)為null,但是,第2到第19的User對(duì)象中的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類似,也需要綁定在對(duì)象上,而不能直接寫在Controller方法的參數(shù)中。但是,綁定Set數(shù)據(jù)時(shí),必須先在Set對(duì)象中add相應(yīng)的數(shù)量的模型對(duì)象。

    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,則會(huì)拋出org.springframework.beans.InvalidPropertyException異常。所以,在使用時(shí)有些不便。暫時(shí)沒找到解決方法,如果有網(wǎng)友知道,請(qǐng)回帖共享你的做法。

5. Map綁定:

    Map最為靈活,它也需要綁定在對(duì)象上,而不能直接寫在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 

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

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

標(biāo)簽:廣元 萍鄉(xiāng) 蚌埠 棗莊 衡水 大理 江蘇 衢州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SpringMVC 數(shù)據(jù)綁定實(shí)例詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266