如何在JSF页面的EL表达式中按索引显示ArrayList的元素

编程入门 行业动态 更新时间:2024-10-23 11:16:48
本文介绍了如何在JSF页面的EL表达式中按索引显示ArrayList的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想将 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(或 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)}

另见:

我们的 EL 维基页面调用直接方法或带参数的方法/变量/EL中的参数

但是我想知道是否只使用迭代列表中所有元素的组件不是更容易,例如 ;,这样您就不需要事先知道大小,也不需要通过索引获取每个单独的项目.例如

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并在 JSF Facelets 中渲染每个项目

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

更多推荐

[db:关键词]

本文发布于:2023-04-16 10:40:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/885001.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   索引   元素   页面   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!