【发布时间】:2012-03-11 12:58:51
【问题描述】:
我想在 JSF 页面中显示 java 数组列表。我从数据库生成了arraylist。现在我想通过按索引号调用列表元素索引来将列表显示到 JSF 页面中。是否可以直接从JSF页面中的EL表达式向bean方法传递参数并显示?
【问题讨论】:
我想在 JSF 页面中显示 java 数组列表。我从数据库生成了arraylist。现在我想通过按索引号调用列表元素索引来将列表显示到 JSF 页面中。是否可以直接从JSF页面中的EL表达式向bean方法传递参数并显示?
【问题讨论】:
您可以使用大括号符号[] 通过特定索引访问列表元素。
@ManagedBean
@RequestScoped
public class Bean {
private List<String> list;
@PostConstruct
public void init() {
list = Arrays.asList("one", "two", "three");
}
public List<String> getList() {
return list;
}
}
#{bean.list[0]}
<br />
#{bean.list[1]}
<br />
#{bean.list[2]}
至于参数传递,当然是可以的。 EL 2.2(或 JBoss EL,当您仍在使用 EL 2.1 时)支持使用参数调用 bean 方法。
#{bean.doSomething(foo, bar)}
然而我想知道是否只使用一个迭代列表中所有元素的组件不是更容易,例如<ui:repeat> 或<h:dataTable>,这样您就不需要事先知道大小,也不需要知道通过索引获取每个单独的项目。例如
<ui:repeat value="#{bean.list}" var="item">
#{item}<br/>
</ui:repeat>
或
<h:dataTable value="#{bean.list}" var="item">
<h:column>#{item}</h:column>
</h:dataTable>
【讨论】: