spring mvc各种常见类型参数绑定方式以及json字符串绑定对象
在使用spring mvc作为框架的时候,为了规范,我们通常希望客户端的请求参数符合规范直接通过DTO的方式从客户端提交到服务端,以便保持规范的一致性,除了很简单的情况使用RequestParam映射外,我们通常使用比较的有以下几种情形:
1、pojo嵌套pojo。
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; } }
@RequestMapping("test.html") 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()); }
<form action="saysth.do" method="post"> <input name="firstName" value="张" /><br> <input name="lastName" value="三" /><br> <input name="contactInfo.tel" value="12121212" /><br> <input name="contactInfo.address" value="测试" /><br> <input type="submit" value="Save" /> </form>
2、pojo嵌套map。
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; } }
@RequestMapping("saysth.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()); } }
<form action="saysth.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>
3、pojo嵌套list。
<form action="saysth.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>
2-19的记录的属性会使用默认值。
其实上述的方式应该算是很早版本spring的用法了,现在一般都使用RequestBody,直接将json字符串绑定到对象上,如下所示:
<script type="text/javascript"> $(document).ready(function(){ var saveDataAry=[]; var data1={"userName":"test","address":"gz"}; var data2={"userName":"ququ","address":"gr"}; saveDataAry.push(data1); saveDataAry.push(data2); $.ajax({ type:"POST", url:"user/saveUser", dataType:"json", contentType:"application/json", // 这跟org.springframework.http.converter.json.MappingJackson2HttpMessageConverter的supportedMediaTypes是一样的含义 data:JSON.stringify(saveData), success:function(data){ } }); }); </script>
@RequestMapping(value = "saveUser", method = {RequestMethod.POST }}) @ResponseBody public void saveUser(@RequestBody List<User> users) { userService.batchSave(users); }
注意:
-
如果请求的表单参数中不存在pojo对象中的某个域名称的项目,则绑定后pojo该属性为null。
-
如果请求的表单参数中存在pojo对象中没有包含的域的项目,则绑定后该参数丢失。
-
如果表单项目的数据会自动从String转换为相应pojo的域类型。但如果类型无法转换,则报错。如果我输入age为dafsdfa,那么报错。