【发布时间】:2019-07-14 09:47:00
【问题描述】:
我正在使用旧版 JSF Web 应用程序,而我的 h:dataTable 元素给我带来了麻烦。通常,它会准确地显示我想要的方式 - 一个标题和几行,所有这些都具有适当的填充和边距以及所有内容。
但是,如果我尝试显示一个零行的表格(这对我来说是一个有效的用例),JSF 仍会呈现一行,尽管内容为空。
这是这个 h:dataTable 的源代码:
<h:dataTable styleClass="table" value="#{backingBean.emptyList}" var="result">
<h:column>
<f:facet name="header">First Column</f:facet>
<h:outputText value="#{result}"/>
</h:column>
<h:column>
<f:facet name="header">Second Column</f:facet>
<h:outputText value="#{result}"/>
</h:column>
<h:column>
<f:facet name="header">Third Column</f:facet>
<h:outputText value="#{result}"/>
</h:column>
</h:dataTable>
这是浏览器正在呈现的内容:
<table class="table">
<thead>
<tr>
<th scope="col">First Column</th>
<th scope="col">Second Column</th>
<th scope="col">Third Column</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
以下是支持 bean 中的方法,它们为我提供了我的结果列表:
public List<String> getEmptyList() { // incorrectly renders 1 empty row
return Collections.emptyList();
}
public List<String> getThreeRows() { // correctly renders 3 rows
return Arrays.asList(new String[] {"row1", "row2", "row3"});
}
为什么 JSF 渲染这个空行?我原以为<tbody> 只是空的。这是 JSF 的正确行为吗?还是我配置错误?
请指教,
-八月
【问题讨论】:
-
因为您的结果集包含一个为空的条目??? minimal reproducible example请
-
我已将问题更新为更简洁。
-
似乎可以重现:即使是一个微不足道的数据表
<h:dataTable><h:column/></h:dataTable>也有一个带有一行的主体:<table> <tbody> <tr><td></td></tr></tbody> </table> -
这就是 JSF2 的工作原理吗?我很惊讶它正在渲染
。也许这是一个有意识的设计决定?或者可能是一个错误? 我无法想象这是一个错误,除非您使用的是最新最好的快照并且这是一个遗漏。