springboot 表单传入后端 List参数

前端form<form><input type-\"text\" value=\"testString\" name=\"userList[0].age\" /

前端form


<form>
<input type-"text" value="testString" name="userList[0].age" />
<input type="submit" value="提交" />
</form>
User.java



package com.qianxiao.blogs.model.entity;

import javax.persistence.*;

@Entity
@Table(name = "dx_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String age;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

UserData.java

package com.qianxiao.blogs.model.data;

import com.qianxiao.blogs.model.entity.User;

import java.util.ArrayList;
import java.util.List;

public class UserData {
    private List<User> users = new ArrayList<>();

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }
}

Controller.java


@GetMapping(value = "getTest")
    @ResponseBody
    public List<User> getTest(
            UserData userData
    ) {
        System.out.println("userList = " + userData.getUsers());
       // 即可正常获取前端传入的数组
        return userData.getUsers();
    }

发表评论

登录 后才能评论