使用c:foreach(JSP/JSTL)遍历ArrayList,变量不起作用 [英] Iterating over an ArrayList with c:foreach (JSP/JSTL), Variable doesn't work

查看:612
本文介绍了使用c:foreach(JSP/JSTL)遍历ArrayList,变量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多例子可以解决我的问题,但是我经历了很多例子,无法弄清楚我的错误在哪里.

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的字符串变量,带有正确的获取器和设置器.

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#valueOf(Object)方法将对象转换为String,并将其直接写入HTTP响应.这不是您想要的.甚至,您应该不要完全将老式的 scriptlet 与现代的标记库/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 Wiki页面
  • See also:

    • How to avoid Java code in JSP files?
    • Our Servlets wiki page - Hello World #2 may be useful to you
    • Our EL wiki page
    • 更新:关于EL无法解释的问题,您显然在JSTL和container/web.xml版本之间不匹配.确保版本正确对齐.例如. Servlet 3.0容器,web.xml中的version="3.0",JSTL 1.2.另请参见我们的JSTL Wiki页面.

      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.

      • 我们的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!"

      这篇关于使用c:foreach(JSP/JSTL)遍历ArrayList,变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆