我正在使用遗留的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的正确行为吗?或者我有什么错误的配置?
请指教,
-八月
根据Mojarra 2.3.8的源代码,这是鼓励行为,TableRenderer
(正如其名称所示)负责和explicitly doing this:
com.sun.faces.renderkit.html_basic.TableRenderer.encodeChildren(FacesContext, UIComponent)
:
if(!renderedRow) {
// if no row with data has been rendered, render that empty row in question:
this.renderEmptyTableRow(writer, data);
}
您的选择包括:
1)向dataTable添加渲染属性:
<h:dataTable value="#{backingBean.entityList}"
rendered="#{not empty backingBean.entityList}" ...>
...
</h:dataTable>
<h:outputText rendered="#{empty backingBean.entityList}"
value="No data to display, sooo sorry :-("/>
2)覆盖TableRenderer
以更好地满足您的需求:
package my;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import com.sun.faces.renderkit.html_basic.TableRenderer;
public class CustomTableRenderer extends TableRenderer {
@Override
public void encodeChildren(final FacesContext context, final UIComponent component) throws IOException {
final UIData data = (UIData) component;
final int rowCount = data.getRowCount();
if (rowCount > 0) {
super.encodeChildren(context, component);
} else {
// do what super.encodeChildren does, but your way ...
}
}
}
可悲的是,你不能只是覆盖com.sun.faces.renderkit.html_basic.TableRenderer.renderEmptyTableRow(ResponseWriter, UIComponent)
并使它无所作为,因为它是private
。
在faces-config.xml
中注册您的自定义渲染器:
<render-kit>
<renderer>
<component-family>javax.faces.Data</component-family>
<renderer-type>javax.faces.Table</renderer-type>
<renderer-class>my.CustomTableRenderer</renderer-class>
</renderer>
</render-kit>
编辑:有问题的行为是引入in a commit修复issue #1009 with comment:
Fix for issue 1009: Rendered h:dataTable/h:panelGrid without rows/content do not validate against XHTML 1.0 Transitional (and html4)
git-svn-id: https://svn.java.net/svn/mojarra~svn/trunk@7669 761efcf2-d34d-6b61-9145-99dcacc3edf1
太糟糕了,我再也找不到那个问题,but @Kukeltje did!