【发布时间】:2018-07-17 19:34:31
【问题描述】:
我是 StackOverflow 的新手,并且讨厌提出问题的想法,因为我看到很多人在火焰中被击落,因为被问得不好或在其他地方得到了回答,但我似乎无法找到答案不管我怎么努力。开始!
我在 Spring-Boot 应用程序中有以下模型:
public class Timesheet {
//some straightforward getters/setters (e.g. id)
public List<TimesheetRow> getTimesheetRow() {
return timesheetRow;
}
public void setTimesheetRow(List<TimesheetRow> timesheetRow) {
this.timesheetRow = timesheetRow;
}
}
public class TimesheetRow {
//some straightforward getters/setters (e.g. rowId)
public List<TimesheetTask> getTimesheetTask() {
return timesheetTask;
}
public void setTimesheetTask(List<TimesheetTask> timesheetTask) {
this.timesheetTask = timesheetTask;
}
}
public class TimesheetTask {
//some straightforward getters/setters (e.g. taskId)
}
希望到目前为止一切顺利,并且我已经编写了服务和 DAO 层来取回适当的数据 - 我已经手动将一些数据写入数据库并验证当我调用访问方法时,正确的数据是被退回。
但是,当我尝试使用 Thymeleaf 呈现这些数据时,就会出现一个问题。到目前为止我的代码是这样的(请原谅糟糕的格式,我只是想在整理表格结构之前让它工作):
<table>
<tr th:each="row,iteration : ${timesheet.timesheetRow}">
<td>
<!--This part actually works. Huzzah!-->
<input type="hidden" th:field="*{timesheetRow[__${iteration.index}__].id}"/>
<input type="text" th:field="*{timesheetRow[__${iteration.index}__].projectId}" />
</td>
<td>
<span th:each="task,iteration2 : ${row.timesheetTask}">
<!--These two lines are particularly poor, and are just my futile attempts at trying different ways to try and reference appropriately. Sorry.-->
<input type="hidden" th:field="*{timesheetRow.get(__${iteration.index}__).getTimesheetTask.get(__${iteration2.index}__).getId()}"/>
<input type="text" th:field="*{task.work}"/>
</span>
</td>
</tr>
</table>
我已尝试根据以下答案自行解决此问题:
nested (double) loop with thymeleaf
How to bind an object list with thymeleaf?
...但是当第一个谈论“深入两层”时,我似乎无法访问第二层,即每个单独的时间表行中的时间表任务。
据我所知,我正在尝试的概念代码类似于
timesheet.timesheetRow[0].timesheetTask[0]
或类似的,但这显然是 Java 无法识别的可怕语法,因此 ThymeLeaf 很有可能对它做任何事情。
使用 ThymeLeaf 可能完全不可能,我正在考虑重构代码以将所有任务字段添加到时间表行,但这可能吗?如果可以,如何实现?
在此先感谢您,如果有任何关于如何改进我的提问技巧的反馈,我将不胜感激!
【问题讨论】:
-
你试过这个语法吗:
*{timesheetRow[__${iteration.index}__].timesheetTask[__${iteration2.index}__].id} -
我确信我已经尝试过了,但它绝对有效!您能否将其添加为答案,以便我接受?
-
非常感谢!
-
没问题,我确定你也试过了,但我只是想确定一下,我问的很好!
标签: java spring spring-mvc thymeleaf