HTL渲染表格列异常:行key正常显示但列列表无法渲染(CRXDE数据正常)
问题排查与解决方案
1. Java模型类缺少Getter方法
Sling Models 默认要求通过公共Getter方法暴露属性,HTL才能访问私有字段。你的实体类中tableRowList、key、tableColumnList、colValue均为私有字段且无对应Getter,导致HTL无法正确读取tableColumnList的数据。
修改后的Java类示例:
import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.ChildResource; import org.apache.sling.models.annotations.ValueMapValue; import java.util.List; @Model(adaptables = Resource.class) public class Table { @ChildResource private List<TableRow> tableRowList; public List<TableRow> getTableRowList() { return tableRowList; } } public class TableRow { @ValueMapValue private String key; @ChildResource private List<TableColumn> tableColumnList; public String getKey() { return key; } public List<TableColumn> getTableColumnList() { return tableColumnList; } } public class TableColumn { @ValueMapValue private String colValue; public String getColValue() { return colValue; } }
2. HTL错误引用对象而非属性
你的HTL代码中,data-sly-repeat.colValue="${item.tableColumnList}"循环的是TableColumn对象列表,但输出时直接写${colValue},会输出对象的toString值而非colValue属性。需修改为访问对象的对应属性:
修改后的HTL代码:
<sly data-sly-use.model="models.Table"> <table class="table"> <tbody> <sly data-sly-list="${model.tableRowList}"> <tr> <th>${item.key @context='html'}</th> <th data-sly-repeat.column="${item.tableColumnList}">${column.colValue @context='html'}</th> </tr> </sly> </tbody> </table> </sly>
3. 额外检查点
- 确认CRXDE中子资源命名与
@ChildResource匹配:如果TableRow节点下的子资源是短横线命名table-column-list而非驼峰tableColumnList,需在注解中指定名称:@ChildResource(name = "table-column-list") private List<TableColumn> tableColumnList; - 确认Sling Models配置正确:在
/apps/[你的项目]/config/org.apache.sling.models.impl.ModelAdapterFactory.config中,确保模型所在包被包含:modelsPackages=["models"]
内容的提问来源于stack exchange,提问作者superninja
相关产品推荐
相关产品推荐

