如何显示JSF页面中通过索引ArrayList的元素 [英] How to display elements of ArrayList by index in JSF page

查看:101
本文介绍了如何显示JSF页面中通过索引ArrayList的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示的java数组列表到JSF页面。我生成数组列表从数据库。现在我想通过调用索引编号列表中的元素的索引以显示列表到JSF页面。是否有可能从JSF页面直接参数传递到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 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(或JBoss的EL,当你仍然在EL 2.1)支持调用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)}


然而,我不知道它是不是更容易只是使用它迭代列表中的所有元素,如℃的组成部分;用户界面:重复&GT; &LT; H:dataTable的&GT; ,这样就不需要事先知道大小也不由指数让每一个单独的项目。例如。


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>

这篇关于如何显示JSF页面中通过索引ArrayList的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆