Iterating over an ArrayList with c:foreach (JSP/JSTL), Variable doesn't work

只谈情不闲聊 提交于 2020-08-05 05:32:18

问题


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.

I am iterating over an ArrayList(TestSzenario). The class TestSzenario contains a String Variable called name with proper getters and setters.

Here's my code:

<td><select name="selectSzenario" id="selectSzenario" size="1">
                <c:forEach items="<%=testszenario.getSzenariosForSummary() %>" var="szenario"> 
                    <option>${szenario.name}</option>
                </c:forEach></select></td></tr>

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-output:

        <tr><td>Szenario:</td>
        <td><select name="selectSzenario" id="selectSzenario" size="1">

                    <option>${szenario.name}</option>

                    <option>${szenario.name}</option>
                </select></td></tr>

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>

But in either case it doesn't even iterate through the List, resulting in only 1 option (the List contains 2 elements).


回答1:


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.

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"> 

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

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.

See also:

  • Our JSTL wiki page - read the section "Help! The expression language (EL, those ${} things) doesn't work in my JSTL tags!"


来源:https://stackoverflow.com/questions/14625738/iterating-over-an-arraylist-with-cforeach-jsp-jstl-variable-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!