h:dataTable始终显示一行;将不显示零行 [英] h:dataTable always displays one row; won't display zero rows
问题描述
我正在使用旧版JSF Web应用程序,而我的h:dataTable元素给我带来了麻烦.通常,它会准确显示我想要的样子-标头和几行,全部带有适当的填充和边距以及所有内容.
I'm working in a legacy JSF web app, and my h:dataTable element is giving me trouble. Normally, it displays exactly how I want it - a header and several rows, all with proper padding and margins and everything.
但是,如果我尝试显示一个零行的表(这对我来说是一个有效的用例),那么JSF仍将呈现一行,尽管其中没有内容.
However, if I try to display a table with zero rows (which is a valid use case for me), JSF still renders one row, albeit empty of contents.
这是此h:dataTable的源代码:
Here's the source code for this 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>
以下是浏览器呈现的内容:
Here's what's being rendered by the browser:
<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的方法,这些方法可以给我我的结果列表:
Here are the methods in the backing bean that give me my result list:
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的正确行为吗?还是我配置错误?
Why is JSF rendering this empty row? I would have expected the <tbody>
to just be empty. Is this the correct behavior for JSF? or do I have something misconfigured?
请告知,
-8月
推荐答案
根据Mojarra 2.3.8的源代码,这是鼓励行为,TableRenderer
(顾名思义)负责,
As per the source code of Mojarra 2.3.8 this is encouraged behavior, the TableRenderer
(as its' name says) is responsible for and 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)将呈现的属性添加到您的数据表:
1) Add a rendered attribute to your 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
以便更好地满足您的需求:
2) Override the TableRenderer
to do the job better fitting your needs:
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
.
Sadly you can not just override com.sun.faces.renderkit.html_basic.TableRenderer.renderEmptyTableRow(ResponseWriter, UIComponent)
and make it do nothing because it is private
.
在faces-config.xml
中注册您的自定义渲染器:
In faces-config.xml
register your custom renderer:
<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>
有问题的行为是在
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
太糟糕了,我再也找不到这个问题了,但@Kukeltje做到了!
Too bad I could not find that issue anymore, but @Kukeltje did!
这篇关于h:dataTable始终显示一行;将不显示零行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!