h:dataTable始终显示一行;将不显示零行

时间:2019-02-20 16:15:47

标签: css jsf jsf-2

我正在使用旧版JSF Web应用程序,而我的h:dataTable元素给我带来了麻烦。通常,它会准确显示我想要的样子-标头和几行,全部带有适当的填充和边距以及所有内容。

h:datatable displaying three rows correctly

但是,如果我尝试显示一个零行的表(这对我来说是一个有效的用例),那么JSF仍将呈现一行,尽管没有内容。

h:dataTable rendering one empty row when it should be rendering zero rows

这是此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的正确行为吗?还是我配置错误?

请告知,

-8月

1 个答案:

答案 0 :(得分:4)

根据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)将呈现的属性添加到您的数据表:

<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!