Entity类:
package com.imooc.object; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="admin") public class Admin { private String name; private Integer age; @XmlElement(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement(name="age") public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Admin{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
package com.imooc.object; import java.util.HashSet; import java.util.Objects; import java.util.Set; public class User { private String name; private Integer age; private ContactInfo contactInfo; public ContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(ContactInfo contactInfo) { this.contactInfo = contactInfo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public static void main(String[] args) { Set<User> u = new HashSet<User>(); User u1 = new User(); u1.setName("aa"); User u2 = new User(); u2.setName("aa"); u.add(u1); u.add(u2); System.out.println("size:"+u.size()); System.out.println(u); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return Objects.equals(name, user.name) && Objects.equals(age, user.age); } @Override public int hashCode() { return Objects.hash(name, age); } // @Override // public String toString() { // return "User{" + // "name='" + name + '\'' + // ", age=" + age + // '}'+this.hashCode(); // } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", contactInfo=" + contactInfo + '}'; } }
package com.imooc.object; public class ContactInfo { private String phone; private String address; public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "ContactInfo{" + "phone='" + phone + '\'' + ", address='" + address + '\'' + '}'; } }
package com.imooc.object; import java.util.LinkedHashSet; import java.util.Set; public class UserSetForm { private Set<User> users; private UserSetForm(){ users = new LinkedHashSet<User>(); users.add(new User()); users.add(new User()); } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } @Override public String toString() { return "UserSetForm{" + "users=" + users + '}'; } }
package com.imooc.object; import java.util.List; public class UserListForm { private List<User> users; public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } @Override public String toString() { return "UserListForm{" + "users=" + users + '}'; } }
package com.imooc.object; import java.util.Map; public class UserMapForm { private Map<String,User> users; @Override public String toString() { return "UserMapForm{" + "users=" + users + '}'; } public Map<String, User> getUsers() { return users; } public void setUsers(Map<String, User> users) { this.users = users; } }
Web类:
package com.imooc.controller; import com.imooc.object.*; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.text.SimpleDateFormat; import java.util.Date; @Controller public class TestController { //todo http://localhost:8080/baseType.do?age=10 //todo 500 http://localhost:8080/baseType.do //todo 400 http://localhost:8080/baseType.do?age=abc @RequestMapping(value = "baseType.do") @ResponseBody public String baseType(int age){ return "age:"+age; } //todo http://localhost:8080/baseType2.do?age=10 //todo http://localhost:8080/baseType2.do //todo 400 http://localhost:8080/baseType2.do?age=abc @RequestMapping(value = "baseType2.do") @ResponseBody public String baseType2(Integer age){ return "age:"+age; } //todo http://localhost:8080/array.do?name=Tom&name=Lucy&name=Jim @RequestMapping(value = "array.do") @ResponseBody public String array(String[] name){ StringBuilder sbf = new StringBuilder(); for(String item : name){ sbf.append(item).append(" "); } return sbf.toString(); } //todo http://localhost:8080/object.do?name=Tom&age=10 //TODO http://localhost:8080/object.do?name=Tom&age=10&contactInfo.phone=10086 //TODO http://localhost:8080/object.do?user.name=Tom&admin.name=Lucy&age=10 @RequestMapping(value = "object.do") @ResponseBody public String object(User user,Admin admin){ return user.toString()+" "+admin.toString(); } @InitBinder("user") public void initUser(WebDataBinder binder){ binder.setFieldDefaultPrefix("user."); } @InitBinder("admin") public void initAdmin(WebDataBinder binder){ binder.setFieldDefaultPrefix("admin."); } //TODO http://localhost:8080/list.do?users[0].name=Tom&users[1].name=Lucy //TODO http://localhost:8080/list.do?users[0].name=Tom&users[1].name=Lucy&users[20].name=Jim @RequestMapping(value = "list.do") @ResponseBody public String list(UserListForm userListForm){ return "listSize:"+userListForm.getUsers().size() + " " + userListForm.toString(); } //TODO http://localhost:8080/set.do?users[0].name=Tom&users[20].name=Lucy @RequestMapping(value = "set.do") @ResponseBody public String set(UserSetForm userSetForm){ return userSetForm.toString(); } //TODO http://localhost:8080/map.do?users['X'].name=Tom&users['X'].age=10&users['Y'].name=Lucy @RequestMapping(value = "map.do") @ResponseBody public String map(UserMapForm userMapForm){ return userMapForm.toString(); } // { // "name": "Jim", // "age": 16, // "contactInfo": { // "address": "beijing", // "phone": "10010" // } // } //application/json @RequestMapping(value = "json.do") @ResponseBody public String json(@RequestBody User user){ return user.toString(); } // <?xml version="1.0" encoding="UTF-8" ?> // <admin> // <name>Jim</name> // <age>16</age> // </admin> //application/xml @RequestMapping(value = "xml.do") @ResponseBody public String xml(@RequestBody Admin admin){ return admin.toString(); }
附:
如果没有@InitBinder注解的辅助方法,根据之前所学的理论,这个方法如果传递user.name,admin.name结果会怎么样呢?
答:user和admin的name属性都为null。
Set集合报错表面取决于提前添加了对应个数的对象,但是底层取决于hashCode方法的判断依据。所以一般情况不建议使用Set来做集合,用List。
待更新...