Dhananjay
Dhananjay

Reputation: 742

c:forEach Iteration

Following is the rough iteration I am Trying:

<c:forEach  items="${row.myList}" var="mainRow" varStatus="table">
        ${ table.first? '<table>':'<tr><td><strong>${mainRow.heading}</strong></td></tr>'}

            <c:forEach items="${mainRow.values}" var="value" >
             <tr>
                <td><input type="checkbox"  value="${value}"/>${value}</td>
              </tr>
            </c:forEach>
             ${ table.last? '</table>':''}
        </c:forEach>

Problem is that it printing ${mainRow.heading} instead of the attribute value. Also what else options are there table.? like first, last. Is there any docs fot it?

Upvotes: 2

Views: 6289

Answers (2)

sbk
sbk

Reputation: 3946

In your code snippet, '<tr><td><strong>${mainRow.heading}</strong></td></tr>' is a just a string as far as JSP is concerned, hence the no substitution. Use this instead

${ table.first? '&lt;table&gt;':'<tr><td><strong>'.concat(mainRow.heading).concat('</strong></td></tr>') }

(I had to use html entities to avoid unmatched tags.)

The other varStatus options are docced here: http://docs.oracle.com/cd/E17802_01/products/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692191

${ table.first? '<table>':'<tr><td><strong>${mainRow.heading}</strong></td></tr>'}

The above expression does not what you want, because you embedded an EL expression inside a String literal inside an EL expression. What you want is

${table.first? '<table>' : '<tr><td><strong>' + mainRow.heading + '</strong></td></tr>'}

or

<c:choose>
    <c:when test="${table.first}">
        <table>
    </c:when>
    <c:otherwise>
        <tr><td><strong>${mainRow.heading}</strong></td></tr>
    </c:otherwise
</c:choose>

which is longer, but more readable, IMO.

Upvotes: 1

Related Questions