问题描述
我想将Java arraylist显示到JSF页面中.我从数据库生成了arraylist.现在,我想通过按索引号调用列表元素来将列表显示到JSF页面中.是否可以直接从JSF页面中的EL表达式将参数传递给bean方法并显示它?
I want to display java arraylist into JSF page. I generated arraylist from database. Now I want to display the list into JSF page by calling the list elements index by index number. Is it possible to pass a parameter to bean method from an EL expression in JSF page directly and display it?
推荐答案
您可以使用大括号符号[]
通过特定索引访问列表元素.
You can access a list element by a specific index using the brace notation []
.
@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(或仍在使用EL 2.1时为JBoss EL)支持通过参数调用bean方法.
As to parameter passing, surely it's possible. EL 2.2 (or JBoss EL when you're still on EL 2.1) supports calling bean methods with arguments.
#{bean.doSomething(foo, bar)}
另请参见:
- 我们的EL Wiki页面
- 调用直接方法或带有参数的方法/EL中的/变量/参数
- Our EL wiki page
- Invoke direct methods or methods with arguments / variables / parameters in EL
See also:
但是我想知道,使用迭代列表中所有元素(例如<ui:repeat>
或<h:dataTable>
)的组件是否更容易,这样您既无需事先知道大小,也不必按索引获取每个单独的项目.例如
I however wonder if it isn't easier to just use a component which iterates over all elements of the list, such as <ui:repeat>
or <h:dataTable>
, so that you don't need to know the size beforehand nor to get every individual item by index. E.g.
<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>
另请参见:
- 如何遍历List< T>并在JSF Facelets中渲染每个项目
- How iterate over List<T> and render each item in JSF Facelets
See also:
这篇关于如何在JSF页面的EL表达式中按索引显示ArrayList的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!