3

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).

olkoza
  • 715
  • 2
  • 17
  • 35

1 Answers1

9

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:


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!"
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You were right, the whole declaration was missing in the web.xml. I didn't read the JSTL Wiki all the way through. Thank you very much, sir! – olkoza Jan 31 '13 at 13:58
  • @BalusC, doesn't the bit about `<%=testszenario.getSzenariosForSummary() %>` converting the object to a string and printing it to the HTTP response only apply to scriptlets *outside* of JSTL tags? See http://stackoverflow.com/questions/93408/test-attribute-in-jstl-cif-tag and http://stackoverflow.com/questions/27684162/how-does-a-scriptlet-pass-an-array-to-a-jstl-tag – mxxk Dec 29 '14 at 06:13