繁体   English   中英

将List-of-List Spring-Boot模型字段与Thymeleaf相关联

[英]Relating List-of-List Spring-Boot Model Fields to Thymeleaf

我是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)

}

希望到目前为止一切顺利,我已经编写了Service和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>

我试图根据以下答案自己解决这个问题:

用百里香叶嵌套(双)环

如何用thymeleaf绑定对象列表?

...但是当第一个谈到“深入两层”时,我似乎无法访问第二层,即每个单独的时间表行中的时间表任务。

据我所知,我正在尝试的概念代码就是这样的

timesheet.timesheetRow[0].timesheetTask[0] 

或类似的,但这显然是Java无法识别的可怕语法,因此ThymeLeaf几乎不可能对它做任何事情。

使用ThymeLeaf这可能完全不可能,我正在考虑重构代码,只是将所有Task字段添加到Timesheet Row,但这可能吗? 如果是这样,它将如何实现?

非常感谢,以及关于如何在黑暗的一天提高我的询问技巧的任何反馈,当我不得不提出另一个问题时,我将不胜感激!

这是正确的语法:

<table>
     <tr th:each="row,iteration : ${timesheet.timesheetRow}">
         <td>
              <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}">
              <input type="hidden" th:field="*{timesheetRow[__${iteration.index}__].timesheetTask[__${iteration2.index}__].id}"/>
              <input type="text" th:field="*{timesheetRow[__${iteration.index}__].timesheetTask[__${iteration2.index}__].work}"/>

         </span>
         </td>
     </tr>
</table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM