简体   繁体   English

如何使用带有EL的另一个列表的值访问列表?

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

I have two Lists. 我有两个清单。 The first list ( numberList) contains two integer elements: [1] and [5] The second list (stringList) contains ten String. 第一个列表(numberList)包含两个整数元素:[1]和[5]。第二个列表(stringList)包含十个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. 我想使用EL使用第一个列表中包含的数字1和5显示第二个列表的第一个和第五个元素。 I'd like to write something like this using EL: 我想使用EL编写如下内容:

<c:out value="${stringList[numberList[0]]}" />
<c:out value="${stringList[numberList[1]]}" />

Is it possible using EL? 可以使用EL吗?

It's perfectly valid. 这是完全有效的。

Assuming the following servlet 假设以下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 和以下的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 您在调用servlet时应该在Web浏览器中看到此信息

one five 一五

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM