gpt4 book ai didi

java - 无法绑定(bind) Spring 表单复选框

转载 作者:行者123 更新时间:2023-11-30 11:04:41 25 4
gpt4 key购买 nike

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

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

这是我目前所拥有的。

Controller

@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>

任何建议

最佳答案

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

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

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 :

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

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

关于java - 无法绑定(bind) Spring 表单复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29950112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com