问题描述
我只需要在JSF/ICEFaces中执行一个非常基本的for
循环,基本上就是显示列号
I simply need to perform a very basic for
cycle in JSF/ICEFaces, basically rendering column numbers
类似下面的伪代码
for(int i=0; i<max; i++)
{
<td>#{i}</td>
}
<c:forEach>
标记遍历集合,但是我不想让我的后备bean更复杂,返回一个愚蠢的整数集合.
the <c:forEach>
tag iterates over collections, but I don't want to make my backing bean more complex returning a stupid collection of integers.
您知道更短更聪明的方法吗?
Do you know a shorter and smarter way?
谢谢
推荐答案
<ui:repeat>
标记是您真正应该使用的标记. JSTL标记在JSF生命周期之外运行. Cay Horstman的JSF课程讨论了这个事实: ui:repeat和处理可变长度数据.
The <ui:repeat>
tag is what you should really use. The JSTL tags operate outside of the JSF Lifecycle. Cay Horstman has a JSF coursewhich discusses this fact: ui:repeat and HandlingVariable-Length Data.
下面提供了一些解决方案,这些解决方案显示出一定的灵活性.您可以执行以下操作:
There are a couple of solutions below which demonstrate some flexibility. You could do something like this:
<ui:param name="max" value="5"/>
<ui:repeat var="i" value="#{indexBean.values}" size="#{max}" >
<tr><td>#{i}</td></tr>
</ui:repeat>
最大行数由名为max
的<ui:parameter>
确定.这不是必需的,但确实显示了灵活性.或者,您可以使用类似以下内容的
The maximum number of rows is determined by a a <ui:parameter>
named max
. This is not required, but does demonstrate flexibility. Alternatively you could use something like:
<ui:param name="max" value="5"/>
<ui:repeat var="i" value="#{indexBean.rowNumbers(max)}">
<tr><td>#{i}</td></tr>
</ui:repeat>
后备bean代码如下:
The backing bean code is the following:
@ManagedBean
public class IndexBean {
public List<Integer> getValues() {
List<Integer> values = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
values.add(i);
}
return values;
}
public List<Integer> rowNumbers(final int max) {
List<Integer> values = new ArrayList<Integer>();
for (int i = 0; i < max; i++) {
values.add(i);
}
return values;
}
}
这篇关于“用于"在JSF中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!