SpringMVC数据绑定一(基本类型、数组和对象(简单对象、层级对象、多参数对象))
SpringMVC数据绑定二(List、Set和Map类型)
@RequestMapping中method的默认值是什么?
没有默认值,如果不配置method,
则以任何请求形式
RequestMethod.GET,
RequestMethod.POST,
RequestMethod.PUT,
RequestMethod.DELETE都可以访问得到。
SpringMVC数据绑定一(基本类型、数组和对象(简单对象、层级对象、多参数对象))
一、int和Integer类型
如:参数为int类型的请求
@Controller public class TestController { @RequestMapping(value="/baseType") @ResponseBody//直接返回字符串界面,不用请求获取jsp页面。去掉报404错误,找不到页面。 public String baseType(int age) { return "age:"+age; } }
1。参数为int类型的请求,若没有传入参数,报500的错误
2.参数为int类型的请求,若参数类型错误,报400的错误
正确
1.2@RequestParam
@Controller public class TestController { @RequestMapping(value="/baseType") @ResponseBody //@RequestParam注解value属性为传参别名,defaultValue默认赋值,required是否必须传值默认true。 //若把required设置为false,不传参则不报错 public String baseType(@RequestParam(value="xage",defaultValue="1",required=false) int age) { return "age:"+age; } }
如:参数为Integer类型
1.若参数请求为Integer类型,没有参数,自动给参数赋值为null
2.若参数请求为Integer类型,参数l类型不匹配,同上也报400错误。
二、参数为数组类型
//http://127.0.0.1:8080/SpringMVC/array?name=Tom&name=Lucy&name=jim @RequestMapping(value="array") @ResponseBody public String array(String[] name) { StringBuilder sbf = new StringBuilder(); for(String item:name) { sbf.append(item).append(" "); } return sbf.toString(); }
三、参数为对象(简单对象)
//model public class User { private String name; private Integer age; //getter and setting ...... @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
//http://127.0.0.1:8080/SpringMVC/object?name=Tom&age=20 @RequestMapping(value="object") @ResponseBody public String object(User user) { return user.toString(); }
三、参数为对象(多层级对象)
1.1model
//联系信息类 public class ContactInfo { private String phone; private String address; //getting and setting............ @Override public String toString() { return "ContactInfo [phone=" + phone + ", address=" + address + "]"; } }
//用户类 public class User { private String name; private Integer age; private ContactInfo contactInfo; //getting and setting............ @Override public String toString() { return "User [name=" + name + ", age=" + age + ", contactInfo=" + contactInfo + "]"; } }
//http://127.0.0.1:8080/SpringMVC/object?name=Tom&age=20&contactInfo.phone=10086&contactInfo.address=ss @RequestMapping(value="object") @ResponseBody public String object(User user) { return user.toString(); }
结果显示:
1.2 若参数为2个对象
@RequestMapping(value="object") @ResponseBody public String object(User user,Admin admin) { return user.toString()+" "+admin.toString(); }
两个参数对象被赋予相同的参数
解决办法@InitBinder
//http://127.0.0.1:8080/SpringMVC/object?user.name=Tom&age=20&admin.name=lucy
@RequestMapping(value="object") @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."); }
SpringMVC数据绑定二(List、Set和Map类型)
1.List类型绑定
//联系信息类 用户类的子属性 public class ContactInfo { private String phone; private String address; //getting and setting............ }
//用户类 public class User { private String name; private Integer age; private ContactInfo contactInfo; //getting and setting............ @Override public String toString() { return "User [name=" + name + ", age=" + age + ", contactInfo=" + contactInfo + "]"; } }
//List<User> 集合的包装类 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 + "]"; } }
//controller方法 //http://127.0.0.1:8080/SpringMVC/list?users[0].name=Tom&users[0].age=20&users[1].name=Lucy&users[1].age=25&users[1].contactInfo.phone=10086 @RequestMapping(value="list") @ResponseBody public String list(UserListForm userListForm) { return userListForm.toString(); }
结果显示:
注意:若参数中users[20] 则生成的list对应的size为21
2.Set
//Set<User> 集合的包装类
public class UserSetForm { private Set<User> users; public UserSetForm() { //必须初始化,如若没有则报500错误Cannot get element with index 0 from Set of size 0
//getPropertyValue(AbstractNestablePropertyAccessor.java:661)中做判断。抛出异常
users = new LinkedHashSet<>(); 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 + "]"; } }
@RequestMapping(value="set") @ResponseBody public String set(UserSetForm userSetForm) { return userSetForm.toString(); }
访问方式和list一样:但是必须在声明的时候初始化,有几个对象初始化几个
3、Map
//Map<String,User> users 包装类 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; } @Override public String toString() { return "UserMapForm [users=" + users + "]"; } }
//TODO:http://127.0.0.1:8080/SpringMVC/map?users["x"].name=Tom&users["x"].age=10&users["y"].name=Lucy @RequestMapping(value="map") @ResponseBody public String map(UserMapForm userMapForm) { return userMapForm.toString(); }