使用 c:foreach (JSP/JSTL) 迭代 ArrayList,变量不起作用 [英] Iterating over an ArrayList with c:foreach (JSP/JSTL), Variable doesn't work
问题描述
我知道有无数的例子可以解决我的问题,但我经历了很多,但无法弄清楚我的错误在哪里.
There are countless examples out there for my problem, I know, but I went through a lot of them and can't figure out where my mistake is.
我正在遍历一个 ArrayList(TestSzenario).类 TestSzenario 包含一个名为 name 的字符串变量,具有适当的 getter 和 setter.
I am iterating over an ArrayList(TestSzenario). The class TestSzenario contains a String Variable called name with proper getters and setters.
这是我的代码:
<td><select name="selectSzenario" id="selectSzenario" size="1">
<c:forEach items="<%=testszenario.getSzenariosForSummary() %>" var="szenario">
<option>${szenario.name}</option>
</c:forEach></select></td></tr>
我的问题是,变量不起作用.对于选择框中的每个选项,我总是得到 ${szenario.name}.我正确地声明了 JSTL-taglib,因为在完成后站点中有多个选项,我知道迭代正在工作.我还查看了 HTML 源代码,解决了 foreach 问题.
My Problem is, the Variable isn't working. I alwas get ${szenario.name} for every option in the select-box. I declared the JSTL-taglib properly and since there are multiple options in the site when done i know the iteration is working. Also I looked in the HTML-sourcecode an the foreach is resolved.
HTML 输出:
<tr><td>Szenario:</td>
<td><select name="selectSzenario" id="selectSzenario" size="1">
<option>${szenario.name}</option>
<option>${szenario.name}</option>
</select></td></tr>
编辑答案 1:谢谢,但我以前试过:
EDIT for answer 1: Thank you, but I tried that before:
ArrayList<TestSzenario> szenarioList = testszenario.getSzenariosForSummary();
request.setAttribute("aList", szenarioList);
request.setAttribute("ts", testszenario);
<c:forEach items="${aList}" var="szenario">
<option>${szenario.name}</option>
</c:forEach></select></td></tr>
<c:forEach items="${ts.szenariosForSummary}" var="szenario">
<option>${szenario.name}</option>
</c:forEach></select></td></tr>
但在任何一种情况下,它甚至都不会遍历 List,导致只有 1 个选项(List 包含 2 个元素).
But in either case it doesn't even iterate through the List, resulting in only 1 option (the List contains 2 elements).
推荐答案
<%=testszenario.getSzenariosForSummary() %>
将对象转换为 String
使用 String#valueOf(Object)
方法并将其直接写入 HTTP 响应.这不是你想要的.更重要的是,您根本不应该不要将老式scriptlets与现代 taglibs/EL 混合在一起.
The <%=testszenario.getSzenariosForSummary() %>
will convert the object to String
using String#valueOf(Object)
method and write it straight to the HTTP response. This is not what you want. Even more, you should not be mixing oldschool scriptlets with modern taglibs/EL at all.
您需要确保 testszenario
可用于 EL ${}
.因此,只需像这样在某些 servlet 中预先将其设置为页面、请求、会话或应用程序范围的属性
You need to make sure that testszenario
is available to EL ${}
. So, just set it as an attribute of page, request, session or application scope beforehand in some servlet like so
request.setAttribute("testszenario", testszenario);
然后你就可以用通常的方式访问它了:
Then you can just access it the usual way:
<c:forEach items="${testszenario.szenariosForSummary}" var="szenario">
另见:
- 如何避免 JSP 文件中的 Java 代码?
- 我们的 Servlets wiki 页面 - Hello World #2 可能对您有用
- 我们的 EL 维基页面
- How to avoid Java code in JSP files?
- Our Servlets wiki page - Hello World #2 may be useful to you
- Our EL wiki page
- 我们的 JSTL wiki 页面 - 阅读帮助!表达式语言(EL,那些
${}
的东西)在我的 JSTL 标签中不起作用!" - Our JSTL wiki page - read the section "Help! The expression language (EL, those
${}
things) doesn't work in my JSTL tags!"
See also:
更新:至于 EL 没有被解释的问题,您显然 JSTL 和容器/web.xml
版本之间不匹配.确保版本正确对齐.例如.Servlet 3.0 容器,web.xml
中的 version="3.0"
,JSTL 1.2.另请参阅我们的 JSTL 维基页面.
Update: as to the problem of EL not being interpreted, you've apparently a mismatch between JSTL and container/web.xml
version. Make sure that the versions are properly aligned. E.g. Servlet 3.0 container, version="3.0"
in web.xml
, JSTL 1.2. See also our JSTL wiki page.
这篇关于使用 c:foreach (JSP/JSTL) 迭代 ArrayList,变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!