http://senton.iteye.com/blog/973918
查看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