我正在尝试将对象推入列表,我无法将值推入正常字段,以下是我使用的绑定值代码:工作模式
public class Job {
public Job(String name) {
super();
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
候选模型
public class Candidate {
private String candidatename;
private List<Job> jobs;
public String getCandidatename() {
return candidatename;
}
public void setCandidatename(String candidatename) {
this.candidatename = candidatename;
}
public List<Job> getJobs() {
return jobs;
}
public void setJobs(List<Job> jobs) {
this.jobs = jobs;
}
}
candidatedetail.html
<form class="form-horizontal" th:action="@{/savejobhistory}" method="post" th:object="${candidate}">
<label>Candidate name:</label>
<input type="text" th:field="*{candidatename}" /><br/>
<button type="submit" class="btn btn-default">Submit</button>
</form>
candidatejob.html
<h2>Candidate Details!</h2>
<span>Note:</span><span th:text="${candidate.candidatename}"></span><br/>
Controller.java
@GetMapping("/emp")
public String test(Model model) {
Candidate candidate = new Candidate();
model.addAttribute("candidate", candidate);
return "candidatedetail";
}
@PostMapping("/savejobhistory")
public String sendForm(@ModelAttribute("candidate") Candidate candidate) {
System.out.println("Inisde Meth");
System.out.println(candidate.getCandidatename());
return "candidatejob";
}
如何推送到列表我尝试遵循,失败
<input type="text"
placeholder='Company Name' class="form-control" th:field="*{candidate.jobs}" th:value="${job.name}"/></td>
<label th:text="${job.name}"></label>
您可以使用以下格式作为示例:
<tr th:each="Jobs : ${yourJobList}">
<td><span th:text="${job.yourjobfield1}"></span></td>
<td><span th:text="${job.yourjobfield2}"></span></td>
</tr>
这将为列表中的每个作业乘以上面的元素块。
本教程可以帮助解释如何在使用表单时索引到数组。th:field="*{jobs[${0}].name}"
不正确。如果你想指定索引,它应该是这样的:
th:field="*{jobs[0].name}"
或者如果你想使用一个变量:
th:field="*{jobs[__${variable}__].name}"
(在这种情况下,必须使用双下划线__
来进行预处理。)