SpringMVC数据绑定

数据绑定即前后端通信的连接部分,前端传一个参数给后端,后端从controller进入执行之后的操作,参数的类型不同也就导致了参数绑定的不同结果

八种基本类型:不能不传(500错误)类型错误(400错误)

    //todo     http://localhost:8088/baseType.do?age=10
    //todo 500 http://localhost:8088/baseType.do
    //todo 400 http://localhost:8088/baseType.do?age=abc
    @RequestMapping(value = "baseType.do")
    @ResponseBody
    public String baseType(int age){
        return "age:"+age;
    }

 

包装类型:建议用这种方式,不传值不保存,因为默认为null

    //todo     http://localhost:8088/baseType2.do?age=10
    //todo     http://localhost:8088/baseType2.do 返回null
    //todo 400 http://localhost:8088/baseType2.do?age=abc
    @RequestMapping(value = "baseType2.do")
    @ResponseBody
    public String baseType2(Integer age){
        return "age:"+age;
    }

 

数组:把每个元素传入即可 如arr=1&arr=2&arr=3即可获得int[] arr={1,2,3};

//todo http://localhost:8088/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();
    }

 

简单对象类型和多层级对象:

简单对象类型(传属性,可以传一部分,其他是默认值(int为0,Integer为null))
多层级对象 (同上,属性用xx.xx的形式 )
传入的两个对象有相同属性时,需要用@InitBinder()注解和方法指定传入的特定前缀(user.和admin.)
   //todo http://localhost:8088/object.do?name=Tom&age=10
    //TODO http://localhost:8088/object.do?name=Tom&age=10&contactInfo.phone=10086
    //TODO http://localhost:8088/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){
        //指定user.属性为user的参数
        binder.setFieldDefaultPrefix("user.");
    }
    @InitBinder("admin")
    public void initAdmin(WebDataBinder binder){
        binder.setFieldDefaultPrefix("admin.");
    }

 

list,set,map:

list绑定时候需要封装list<User> 传入时通过索引指定,索引要连续,否则按最大处理
  //TODO http://localhost:8088/list.do?users[0].name=Tom&users[1].name=Lucy size为2
    //TODO http://localhost:8088/list.do?users[0].name=Tom&users[1].name=Lucy&users[20].name=Jim size为21
    @RequestMapping(value = "list.do")
    @ResponseBody
    public String list(UserListForm userListForm){
        return "listSize:"+userListForm.getUsers().size() + "  " + userListForm.toString();
    }



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 +
                '}';
    }
}
set使用比较复杂,需要提前初始化对象(把几个对象先放到set里,这样就指定了size)
还要重写这个泛型对象的hashcode()和equals()方法,所以一般不用
/TODO http://localhost:8088/set.do?users[0].name=Tom&users[20].name=Lucy
    @RequestMapping(value = "set.do")
    @ResponseBody
    public String set(UserSetForm userSetForm){
        return userSetForm.toString();
    }

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 +
                '}';
    }
}
map绑定用maps['X'].key=**&maps['X'].value=**的形式,其他不用管,同list
//TODO http://localhost:8088/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();
    }

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;
    }
}

 

json:

传json的话用@RequestBody注解即可,
把json放在requesetBody里,就是一个post请求,参数在Body里面,url看不到
需要jackson包的支持
@RequestMapping(value = "json.do")
    @ResponseBody
    public String json(@RequestBody User user){
        return user.toString();
    }


<dependency>
     <groupId>org.codehaus.jackson</groupId>
     <artifactId>jackson-mapper-asl</artifactId>
     <version>1.9.9</version>
 </dependency>

 

xml:

需要oxm包的支持
在对象的类声明前加上@XmlRootElement(name="admin")
在类属性的get()方法前加上@XmlElement(name="name")  @XmlElement(name="age")
@RequestMapping(value = "xml.do")
    @ResponseBody
    public String xml(@RequestBody Admin admin){
        return admin.toString();
    }


@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 +
                '}';
    }
}

 

posted @ 2017-11-22 14:55 雪浪snowWave 阅读(...) 评论(...) 编辑 收藏