无法绑定 spring 表单复选框

Unable to bind spring form checkbox

我正在尝试显示全名,以及主管和经理的复选框。如果此人的价值观为真,则选中这两个复选框。

我想做的是显示具有全名的用户以及具有复选框的主管和经理列,这些列已选中或未选中,并且在提交表单时也会绑定更改。我尝试了模型属性,但没有任何运气。

这是我目前的情况。

控制器

@Controller
@RequestMapping("/person.html")
public class PersonsController {

    @Autowired
    private PersonService personService;

    @RequestMapping(method = RequestMethod.GET)
    public String initForm(@ModelAttribute("personView")PersonView personView, BindingResult result, Model model) {


        List<Person>  persons= personService.getPersonList();

        PersonView personData = new PersonView();
        personView.setPersonList(persons);


        model.addAttribute("personView", personData);   

        return "member";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String submitForm(Model model, PersonView personView, BindingResult result) {
        System.out.println("Controller runs");
        model.addAttribute("persons", personView);
        return "successMember";
    }

}

查看

public class PersonView {

    private boolean isSupervisor;
    private boolean isManager;
    private List<Person> personList;


    public boolean isSupervisor() {
        return isSupervisor;
    }

    public void setSupervisor(boolean isSupervisor) {
        this.isSupervisor = isSupervisor;
    }

    public boolean isManager() {
        return isManager;
    }

    public void setManager(boolean isManager) {
        this.isManager = isManager;
    }

    public List<Person> getPersonList() {
        return personList;
    }

    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }

}

public class Person {

  private String fullName; 
  private Boolean isSupervisor;
  private Boolean isManager;

public String getFullName() {
    return fullName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

public Boolean getIsSupervisor() {
    return isSupervisor;
}

public void setIsSupervisor(Boolean isSupervisor) {
    this.isSupervisor = isSupervisor;
}

public Boolean getIsManager() {
    return isManager;
}

public void setIsManager(Boolean isManager) {
    this.isManager = isManager;
}

}

JSP

        <html>
        <title>Persons Information</title>
        </head>

        <body>

            <form:form method="POST" modelAttribute="personView">
                <table>
                    <tr>
                        <th>Full Name</th>
                        <th>Supervisor</th>
                        <th>Manager</th>
                    </tr>

                    <c:forEach var="person" items="${personView.personList}"
                        varStatus="row">
                        <tr>
                            <td>{person.fullName}</td>
                            <td><form:checkbox path="isSupervisor"
                                    value="${person.isSupervisor}" /></td>
                            <td><form:checkbox path="isManager"
                                    value="${person.isManager}" /></td>
                        </tr>
                    </c:forEach>


                    <tr>
                        <td><input type="submit" name="submit" value="Submit">
                        </td>
                    </tr>
                    <tr>
                </table>
            </form:form>

        </body>
        </html>

任何建议

这里的问题是需要给列表中对应的人绑定参数。如果您按原样查看生成的 HTML,您会发现复选框的名称属性对于所有复选框都是相同的,即在提交表单时无法确定哪个参数适用于哪个人。

因为你不能绑定到原始列表,所以你需要保留包装器:但是它只需要如下所示:

public class PersonView {

    private List<Person> personList;

    public List<Person> getPersonList() {
        return personList;
    }

    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }
}

稍有调整的人:

public class Person {

    private String fullName; 
    private Boolean isSupervisor;
    private Boolean isManager;

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Boolean isSupervisor() {
        return isSupervisor;
    }

    public void setSupervisor(Boolean isSupervisor) {
        this.isSupervisor = isSupervisor;
    }

    public Boolean isManager() {
        return isManager;
    }

    public void setManager(Boolean isManager) {
        this.isManager = isManager;
    }
}

您的 JSP 循环需要如下所示:

<c:forEach var="person" items="${personView.personList}" varStatus="row">
    <tr>
        <td>{person.fullName}</td>
        <td><form:checkbox path="personList[row.index].supervisor"
                value="true" /></td>
        <td><form:checkbox path="personList[row.index].manager"
                value="true" /></td>
    </tr>
</c:forEach>

您的控制器:

@Controller
@RequestMapping("/person.html")
public class PersonsController {

    @Autowired
    private PersonService personService;

    @RequestMapping(method = RequestMethod.GET)
    public String initForm() {
        return "member";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String submitForm(@ModelAttribute PersonView personView) {
        model.addAttribute("persons", personView);
        return "successMember";
    }

    @ModelAttribute
    public PersonView getPersonView(){
        List<Person>  persons= personService.getPersonList();
        PersonView pv = new PersonView();
        pv.setPersonList(persons);

        return pv;
    }
}

**所有这一切都取决于表单处理程序中列表的顺序与为呈现表单而加载的顺序相同,并且同时没有添加任何人。