我在两个列表上循环,并希望将对象添加到一个自定义对象中。因此,我从Thymeleaf中获取一个Employee
列表和一个Doubles
列表,并将它们保存到数据库中。我希望将它们保存到对象EmployeeProject
中,该对象接受一个Employee,一个项目和一个双精度(employeeBookedMonths)。我的问题是,我要么将多个项目保存到EmployeeProject,但所有项目月份都是相同的(列表中最后一个双精度值1)或者我将多个项目保存到employeeProject中,employeeBookedMonths正确,但存在重复项,例如,如果我保存了3个项目,其中包含3个employeeBookedMonths,我得到3次每次保存(所以9保存)。我已经尝试移动employeeProjectService.saveEmployeeProject
,但不能使它工作加上许多其他变化,但我需要一些帮助。
问:我如何循环遍历这两个列表,并将它们添加到EmployeeProject对象中?不获取倍数,或者只获取最后一个employeeBookedMonths。
先谢谢你。
此外,System.println
打印所需的值,它们只是不保存。
public String saveEmployee(@ModelAttribute("employee") Employee employee,
@RequestParam("projectId") List<Project> projectIds,
@RequestParam("employeeProjectMonths") List<Double> months) {
List<Double> monthList = new ArrayList<>();
for (Double month : months) {
if (month != null) {
monthList.add(month);
System.out.println("Month:" + month);
}
}
employeeService.saveEmployee(employee);
if (projectIds != null) {
EmployeeProject employeeProject = new EmployeeProject(employee);
for (Project ids : projectIds) {
for (Double month : monthList) {
employeeProject.setEmployeeBookedMonths(month);
System.out.println("Months: " + employeeProject.getEmployeeBookedMonths());
employeeProjectService.saveEmployeeProject(employee, ids, month);
}
}
}
return "redirect:/ines/employees";
}
新员工表单
<form action="#" th:action="@{/ines/saveEmployeeMeeting}" th:object="${employee}"
method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Email:</label>
<input type="text" th:field="*{name}"
placeholder="Employee Name" class="form-control mb-4 col-4">
</div>
<th:block th:object="${meetingInfo}">
<div class="form-group">
<label>Start Time:</label>
<div class="input-group date" id="datetimepicker1" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input"
th:field="*{meetingStartDateTime}" id="meetingStartDateTime"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="form-group">
<label>End Time:</label>
<div class="input-group date" id="datetimepicker2" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1"
th:field="*{meetingEndDateTime}" id="meetingEndDateTime" placeholder="Date"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="form-group">
<input type="text" th:field="*{message}"
placeholder="Message" class="form-control mb-4 col-4">
</div>
</th:block>
<button type="submit" class="btn btn-info col-2">Save Employee/Meeting</button>
</form>
2条答案
按热度按时间7d7tgy0s1#
在此行中:
它应该保存
employee
还是employeeProject
?因为在代码中你没有显式地使用变量employeeProject
。它应该保存在哪里?ukdjmx9f2#
您是否考虑过您在
if (projectIds != null) {
作用域中声明EmployeeProject employeeProject = new EmployeeProject(employee);
,因此如果您在employeeProject
中执行println,您将看不到它,就在return "redirect:/ines/employees";
的正上方(作用域之外)?为了不丢失数据,您需要在方法的开头声明employeeProject
。以下代码可以更正此问题:
它有帮助吗?如果没有,请张贴您的代码的其余部分。