Html 胸腺套集<;对象>;

Html 胸腺套集<;对象>;,html,thymeleaf,nested-table,Html,Thymeleaf,Nested Table,我在访问嵌套对象集时遇到问题。 我定义了以下对象: @Getter @Setter @AllArgsConstructor @NoArgsConstructor @Entity @Table(name = "site") public class Site { @Id @GeneratedValue(strategy = GerationType.IDENTITY) @Column(name="id", updatable=false,nullable=false)

我在访问嵌套对象集时遇到问题。 我定义了以下对象:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "site")
public class Site {
    @Id
    @GeneratedValue(strategy = GerationType.IDENTITY)
    @Column(name="id", updatable=false,nullable=false)
    private Long id;

    private String siteName;
    private String siteLocation;

    @OneToMany(cascade=CascadeType.ALL, mappedBy = "site")
    private Set<Rack> rack = new HashSet<>();
}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "rack")
public class Rack { 

    @Id
    @GeneratedValue(strategy = GerationType.IDENTITY)
    @Column(name="id", updatable=false,nullable=false)
    private Long id;
    private String rackName;
    private String rackAssetTag;
    private String rackCMDBCode;

    @ManyToOne
    @JoinColumn(name = "site_id")
    private Site site;

    @OneToMany(cascade=CascadeType.ALL, mappedBy = "box")
    private Set<Box> box = new HashSet<>();
}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "box")
public class Box {  
    @Id
    @GeneratedValue(strategy = GerationType.IDENTITY)
    @Column(name="id", updatable=false,nullable=false)
    private Long id;
    private boxAssetTag;
    private boxCMDBCode;

    ManyToOne
    @JoinColumn(name = "rack_id")
    private Rack rack;

}
@Getter
@塞特
@AllArgsConstructor
@诺尔格构装师
@实体
@表(name=“site”)
公共类网站{
@身份证
@GeneratedValue(策略=GerationType.IDENTITY)
@列(name=“id”,updateable=false,nullable=false)
私人长id;
私有字符串siteName;
私有字符串位置;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“site”)
private Set rack=new HashSet();
}
@吸气剂
@塞特
@AllArgsConstructor
@诺尔格构装师
@实体
@表(name=“rack”)
公共类机架{
@身份证
@GeneratedValue(策略=GerationType.IDENTITY)
@列(name=“id”,updateable=false,nullable=false)
私人长id;
私有字符串名称;
私有字符串rackAssetTag;
私有字符串rackCMDBCode;
@许多酮
@JoinColumn(name=“site\u id”)
私人地盘;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“box”)
private Set box=new HashSet();
}
@吸气剂
@塞特
@AllArgsConstructor
@诺尔格构装师
@实体
@表(name=“box”)
公共类框{
@身份证
@GeneratedValue(策略=GerationType.IDENTITY)
@列(name=“id”,updateable=false,nullable=false)
私人长id;
私人包厢;
专用boxCMDBCode;
多对一
@JoinColumn(name=“机架id”)
私人货架;
}
所有关系映射工作都是最高级的。 问题是,当我想为此创建一个好的嵌套表时(css格式化和条件thymeleaf验证被删除,因为这是不相关的):


机架名称
机架资产标签
机架CMDB代码
框资产标签
Box CMDB代码
控制器将一个对象“站点”添加到保存所有关系的模型中。 访问我收到的页面时出错: 在null上找不到属性或字段“box”

我认为当我移动到第二个表时,thymeleaf会释放在外部表中创建的对象框架的上下文。因此,当我尝试调用内部表中的th:each时,没有要执行${rack.box}的rack对象。 问题是如何在不丢失上面对象的上下文的情况下访问thymeleaf中的“更深”对象

问候,,
Jarek.

好的,我已经设法找到了一个解决方案。 我会写下来的。也许有一天会有人需要它

因此,我们的想法是在每个对象上循环,但在主体元素上循环,而不是在行上循环。这使您可以更广泛地了解对象的上下文

<div>
<table>
    <thead>
        <th>Rack name</th>
        <th>Rack asset tag</th>
        <th>Rack CMDB code</th>
    </thead>
    <tbody th:each="rack:${site.rack}">
        <tr>
            <td th:text="${rack.rackName}"></td>
            <td th:text="${rack.rackAssetTag}"></td>
            <td th:text="${rack.rackCMDBCode}"></td>
        </tr>
        <tr>
            <td>
                <table>
                    <thead>
                        <tr>
                            <th>Box asset tag</th>
                            <th>Box CMDB code</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr th:each="box:${rack.box}">
                            <td th:text="${box.boxAssetTag}">
                            <td th:text="${box.boxCMDBCode}">
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

机架名称
机架资产标签
机架CMDB代码
框资产标签
Box CMDB代码

好的,我已经设法找到了一个解决方案。 我会写下来的。也许有一天会有人需要它

因此,我们的想法是在每个对象上循环,但在主体元素上循环,而不是在行上循环。这使您可以更广泛地了解对象的上下文

<div>
<table>
    <thead>
        <th>Rack name</th>
        <th>Rack asset tag</th>
        <th>Rack CMDB code</th>
    </thead>
    <tbody th:each="rack:${site.rack}">
        <tr>
            <td th:text="${rack.rackName}"></td>
            <td th:text="${rack.rackAssetTag}"></td>
            <td th:text="${rack.rackCMDBCode}"></td>
        </tr>
        <tr>
            <td>
                <table>
                    <thead>
                        <tr>
                            <th>Box asset tag</th>
                            <th>Box CMDB code</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr th:each="box:${rack.box}">
                            <td th:text="${box.boxAssetTag}">
                            <td th:text="${box.boxCMDBCode}">
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

机架名称
机架资产标签
机架CMDB代码
框资产标签
Box CMDB代码