Marcio Luiz Scharam
Marcio Luiz Scharam

Reputation: 1

(bug) foreach jsp creating an empty tr tag on each iteration

This is my jsp code:

<table class="table table-striped">
    <thead>
        <tr>
            <th>
                Column name
            </th>
    </tr>
    </thead>
    <tbody>
        <c:forEach var="question" items="${questions}"> 
            <tr>
                <td>
                    <h4>${question.sequence} - ${question.questionText}</h4>
                    <c:forEach var="answer" items="${question.answers}">
                        <div class='answer'> 
                            <input type='radio' name="question${question.id}" id='${answer.id}'/>
                            <label for='${answer.id}'> ${answer.answerText}</label>
                        </div>
                    </c:forEach>
                </td>
            <tr>
        </c:forEach>
    </tbody>
</table>

When the page is printed, this code is creating an empty tr tag after each iteration and the result on html is:

<tbody>
    <tr>
        <td>
            <h4>1 - Question 1?</h4>
            <div class="answer"> 
                <input type="radio" name="question1" id="1">
                <label for="1">Answer 1.</label>
            </div>
            <div class="answer"> 
                <input type="radio" name="question1" id="2">
                <label for="2">Answer 2.</label>
            </div>
        </td>
    </tr>
    <tr>
            </tr>
 ...... (some more iterations with the same result - a question and a empty tr tag)
</tbody>

Anyone knows what's happening with my code??

Upvotes: 0

Views: 421

Answers (1)

akr
akr

Reputation: 739

You are not closing the tr in the loop. You are missing the / in the closing tr. The second <tr> is causing the new row to be output.

Upvotes: 1

Related Questions