Home SpringMVC接收form表单提交的数组数据
Post
Cancel

SpringMVC接收form表单提交的数组数据

前端页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form action="test"  method="post" enctype="application/x-www-form-urlencoded">
            <table>
                <tr>
                    <td><input type="text" name="points[0].x"/></td>
                    <td><input type="text" name="points[0].y"/></td>
                </tr>
                <tr>
                    <td><input type="text" name="points[1].x"/></td>
                    <td><input type="text" name="points[1].y"/></td>
                </tr>
                <tr>
                    <td><input type="text" name="points[2].x"/></td>
                    <td><input type="text" name="points[2].y"/></td>
                </tr>
                <input type="submit" value="Submit">
            </table>
        </form>
    </body>
</html>

实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Point {
    private Integer x;
    private Integer y;

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }
}

实体类集合封装类

SpringMVC不能用List接收,但可以用包装了List成员的对象接收

1
2
3
4
5
6
7
8
9
10
11
public class PointModel {
    private List<Point> points;

    public List<Point> getPoints() {
        return points;
    }

    public void setPoints(List<Point> points) {
        this.points = points;
    }
}

控制器

1
2
3
4
5
6
7
8
9
@Controller
public class PointController {
    @PostMapping("/test")
    @ResponseBody
    public String testPost(PointModel pointModel){
        System.out.println(JSON.toJSONString(pointModel));
        return JSON.toJSONString(pointModel);
    }
}
This post is licensed under CC BY 4.0 by the author.