Java 带有动态列的数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2951729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataTable with dynamic columns
提问by DaveyDaveDave
I am completely new to JSF, and just attempting a proof of concept to decide whether it will be useful for a project. My POC simply consists of a single page, with a table, containing some data.
我对 JSF 完全陌生,只是尝试进行概念验证来决定它是否对项目有用。我的 POC 仅由一个页面和一个包含一些数据的表格组成。
The number of columns (as well as the number of rows) is dynamic, loaded from a database before the page is rendered.
列数(以及行数)是动态的,在呈现页面之前从数据库加载。
With the following, I get two static columns with the appropriate number of rows, as I'd expect:
通过以下内容,我得到了两个具有适当行数的静态列,正如我所期望的:
<h:dataTable id="data" value="#{viewDataBean.dataRows}" var="row">
<h:column>
<f:facet name="header">
<h:outputText value="Col 1"/>
</f:facet>
<h:outputText value="#{row.values[0].value}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Col 2"/>
</f:facet>
<h:outputText value="#{row.values[1].value}"/>
</h:column>
</h:dataTable>
What I wanted to do was to add a <c:forEach...>
around a single set of <h:column>...</h:column>
tags, to iterate over the number of columns, but that didn't work - I tried a variety of combinations, but I was expecting something like this to work:
我想要做的是<c:forEach...>
在一组<h:column>...</h:column>
标签周围添加一个标签,以遍历列数,但这不起作用 - 我尝试了多种组合,但我期待这样的工作:
<h:dataTable id="data" value="#{viewDataBean.dataRows}" var="row">
<c:forEach items="#{row.values}" var="val">
<h:column>
<f:facet name="header">
<h:outputText value="Col 1"/>
</f:facet>
<h:outputText value="#{val.value}"/>
</h:column>
</c:forEach>
</h:dataTable>
From Googling, I've read various vague comments like 'that's not the way to do it', but I haven't been able to find any particularly compelling examples of the right way. Someone mentioned building the DataTable in the backing bean, but the only example of that I could find was http://forums.sun.com/thread.jspa?threadID=577589. It worked, but felt kind of clumsy, especially since some of the methods used are deprecated.
在谷歌搜索中,我阅读了各种模糊的评论,例如“这不是这样做的方式”,但我找不到任何特别引人注目的正确方法的例子。有人提到在支持 bean 中构建 DataTable,但我能找到的唯一示例是http://forums.sun.com/thread.jspa?threadID=577589。它有效,但感觉有点笨拙,特别是因为使用的某些方法已被弃用。
At the moment, it's looking unlikely that I'll be able to use any libraries other than core JSF, but that might change if absolutely necessary. Can anyone shed any light on the right way to do this? It seems like it should be pretty simple, so I'm sure I'm just missing something obvious.
目前,我似乎不太可能使用核心 JSF 以外的任何库,但如果绝对必要,这可能会改变。任何人都可以阐明正确的方法吗?看起来它应该很简单,所以我确定我只是遗漏了一些明显的东西。
采纳答案by BalusC
JSTL and JSF doesn't run in sync as you'd expect from the coding. It's more so that JSTL first processes the entire page from top to bottom first and then hands over the result to JSF for further processing. The #{row}
is unavailable at the time JSTL is busy, so your attempt indeed won't work.
JSTL 和 JSF 不会像您对编码所期望的那样同步运行。更重要的是JSTL先从上到下处理整个页面,然后再将结果交给JSF做进一步处理。该#{row}
是JSTL忙时不可用,那么你确实企图将无法正常工作。
The linked topic indeed mentions deprecated methods, but the topic is also aged and discusses legacy JSF 1.0/1.1. If you explore the current Javadocs of the mentioned methods, you'll see that -as usual- the replacement methods are mentioned. For example, Application#createValueBinding()
mentions the following:
链接的主题确实提到了已弃用的方法,但该主题也已过时并讨论了遗留的 JSF 1.0/1.1。如果您浏览上述方法的当前 Javadocs,您会看到 - 像往常一样 - 提到了替换方法。例如,Application#createValueBinding()
提到以下内容:
Deprecated. This has been replaced by calling
getExpressionFactory()
thenExpressionFactory.createValueExpression(javax.el.ELContext, java.lang.String, java.lang.Class)
.
已弃用。这已被调用
getExpressionFactory()
then取代ExpressionFactory.createValueExpression(javax.el.ELContext, java.lang.String, java.lang.Class)
。
You can find some concrete examples of dynamically populating a datatable this way in this article.
你可以找到的动态填充一个DataTable这种方式在一些具体的例子这篇文章。
As to 3rd party component libraries, RichFaces has a rich:columns
component which is designed for exactly this purpose.
至于 3rd 方组件库,RichFaces 有一个rich:columns
专门为此目的而设计的组件。