SpringMVC 数据绑定实例详解
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
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: python print输出延时,让其立刻输出的方法
下一篇: pytorch实现图像识别(实战)