开发者

How can I access a List using the value of another List with EL?

开发者 https://www.devze.com 2023-03-01 09:18 出处:网络
I have two Lists. The first list ( numberList) contains two integer elements: [1] and [5] The secondlist (stringList) contains ten String.

I have two Lists. The first list ( numberList) contains two integer elements: [1] and [5] The second list (stringList) contains ten String. I'd like to use EL to display the first and the fifth element of the second list using the number 1 and 5 contained in the first one. I'd like to write something like this using EL:

开发者_开发技巧<c:out value="${stringList[numberList[0]]}" />
<c:out value="${stringList[numberList[1]]}" />

Is it possible using EL?


It's perfectly valid.

Assuming the following servlet

List<Integer> numberList = Arrays.asList(0, 4);
request.setAttribute("numberList", numberList);
List<String> stringList = Arrays.asList("one", "two", "three", "four", "five");
request.setAttribute("stringList", stringList);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

and the following JSP

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:out value="${stringList[numberList[0]]}" />
<c:out value="${stringList[numberList[1]]}" />

you should see this in your webbrowser when invoking the servlet

one five

0

精彩评论

暂无评论...
验证码 换一张
取 消