SpringMVC数据绑定

来源:互联网 发布:域名备案要钱吗 编辑:程序博客网 时间:2024/06/14 14:21

1、基本类型:
基本类型数据必须传值,不然会报错

    //   http://localhost:8080/OKAir/Test/Test0.action?age=10    @RequestMapping(value="/Test0",method= {RequestMethod.GET})    public void Test1(@RequestParam(value = "age") int age) {        System.out.println("-----------------Test0--------------");        System.out.println("age:" + age);        System.out.println("-----------------Test0--------------");    }

2、包装类型:
包装类型可以不传参数的值,SpringMVC会自动设值为空

    //  http://localhost:8080/OKAir/Test/Test1.action?age=10    @RequestMapping(value="/Test1",method= {RequestMethod.GET})    public void Test2(@RequestParam(value="age") Integer age) {        System.out.println("-----------------Test1--------------");        System.out.println("age:" + age);        System.out.println("-----------------Test1--------------");    }

3、自定义类:
自定义类需要请求传入的参数的名字和自定义类的属性名字一致
例如一个Student类,有String name 、int age 访问如下:

    //  http://localhost:8080/OKAir/Test/Test2.action?name="小明"&age=10    @RequestMapping(value="/Test2",method= {RequestMethod.GET})    public void Test(Student stu) {        System.out.println("-----------------Test2--------------");        System.out.println(stu);        System.out.println("-----------------Test2--------------"); }

4、自定义复合类:
在自定义符合类中,主类名字对应,内部包含的自定义类用类对象.属性赋值

    //http://localhost:8080/OKAir/Test/Test3.action?age=10&name="小明"&tea.name="张老师"&tea.age=25    @RequestMapping(value="/Test3",method= {RequestMethod.GET})    public void Test3(Student student) {        System.out.println("-----------------Test3--------------");        System.out.println(student);        System.out.println("-----------------Test3--------------");    }

5、List绑定:
List绑定需要声明一个StudentList类 类中定义List< Student > student 之后配合数组下标.属性进行赋值
如student[0].name
此处需要注意:当传入的下标有跨度时候会造成内存的浪费例如:

student[0].name="小明"&student[0].age=15&student[10].name="小明"&student[10].age=15

在后台会生成11个student对象。只有下标为0和10的对象存在值 其他都为空,
StudentList定义:

package com.iss.entity;import java.util.LinkedList;import java.util.List;public class StudentList {    private List<Student> student = new LinkedList<Student>();    public StudentList(List<Student> student) {        super();        this.student = student;    }    public List<Student> getStudent() {        return student;    }    public StudentList() {        super();    }    public void setStudent(List<Student> student) {        this.student = student;    }    @Override    public String toString() {        return "SdudentList [student=" + student + "]";    }}
//  http://localhost:8080/OKAir/Test/Test4.action?student[0].name="小明"&student[0].age=15&student[1].name="小明"&student[1].age=15    @RequestMapping(value="/Test4",method= {RequestMethod.GET})    public void Test4(StudentList student) {        System.out.println("-----------------Test4--------------");        for (Student user : student.getStudent()) {            System.out.println(user);        }        System.out.println("-----------------Test4--------------");    }

6、Map绑定:

和List访问相似,只不过下标变成了字符

//  http://localhost:8080/OKAir/Test/Test5.action?student['a'].age=10&student['a'].name='liu'&student['b'].age=15&student['b'].name='zhang'    @RequestMapping(value="/Test5",method= {RequestMethod.GET})    public void Test5(StudentMap student) {        System.out.println("-----------------Test5--------------");        Set set = student.getStudent().keySet();        Iterator it = set.iterator();        while(it.hasNext()) {            System.out.println(student.getStudent().get(it.next()));        }        System.out.println("-----------------Test5--------------");    }