Get specific element in a list or array using EL
Asked Answered
G

1

36

Is it possible to get specific element in list or array using EL in a Java EE page (Facelets or JSP), or do I have to create a custom EL method?

Gospel answered 26/12, 2011 at 16:5 Comment(0)
G
70

You can use the brace notation [] wherein you specify the (zero-based) index of the element you'd like to retrieve.

<p>This is the 3rd item of the list: #{bean.list[2]}</p>

This syntax does basically the same as bean.getList().get(2).

This is equivalent for arrays.

<p>This is the 3rd item of the array: #{bean.array[2]}</p>

This syntax does basically the same as bean.getArray()[2].

See also:

Gaud answered 27/12, 2011 at 2:19 Comment(2)
Nice! I initially thought this was only applicable for Maps only. Thanks!Gruver
@Fritz: it's applicable on bean properties as well like #{bean[propertyName]}.Gaud

© 2022 - 2024 — McMap. All rights reserved.