Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c:forEach loop printing square bracket delimiters

Tags:

java

jsp

jstl

I am passing an attribute named dataTable into my JSP that is a list of lists of strings. In the JSP if I output the attribute, using ${dataTable} it prints it out in raw format:

[[Header1, Header2, Header3], [A, B, C], [1, 2, 3]]

I can print an item directly like this:

${dataTable[1][2]} 

which outputs: C

Then when I wrote a nested c:forEach loop to print the table, the square brackets at the end of each row are included, but not the square brackets on the entire object.

My code for that is:

<c:forEach var="row" items="${dataTable}">
    <c:forEach var="item" items="${row} ">
        <span>${item}</span>
    </c:forEach>
    <br />
</c:forEach>

Which results in:

        <span>[Header1</span>
        <span> Header2</span>
        <span> Header3] </span>
    <br />
        <span>[A</span>
        <span> B</span>
        <span> C] </span>
    <br />
        <span>[1</span>
        <span> 2</span>
        <span> 3] </span>
    <br />

That is almost what I am going for, I just do not want the square brackets at the beginning and end of every row.

What am I missing which is causing those square brackets to show up when iterating through, but not when I access an item directly?

Thanks!

like image 237
Jon H Avatar asked Mar 27 '13 13:03

Jon H


1 Answers

There's an extra space after ${row} that's causing the list to get "toStringed" on this line <c:forEach var="item" items="${row} ">, change it to <c:forEach var="item" items="${row}"> and you'll be good to go.

like image 50
clav Avatar answered Nov 12 '22 12:11

clav