user1517010
user1517010

Reputation: 23

to display a value of nested list in a list on jsp

I am trying to iterate a list of pojos on jsp using c:forEach syantax. Now the problem is that list contains a nested list ,so how should i display that aparticular value on jsp.

Here is my code on jsp :

<c:forEach items="${capQues.captureQuestionList}" var="captureQuestion" varStatus="status">
  <fieldset name="captureQuestionList[${status.index}].languageId" value="1">
    <legend><c:out value="${captureQuestion.languages}" /></legend>
    <div class="question"><textarea class="textarea" name="captureQuestionList[${status.index}].question" value="question"></textarea></div>
  </fieldset>
</c:forEach>

Where languages is also a list inside captureQuestionList.

Thanks in advance

Upvotes: 0

Views: 6356

Answers (1)

Jasper de Vries
Jasper de Vries

Reputation: 20188

I think what you are missing here is the point of var. In your first loop captureQuestion will be the current object coming from the list captureQuestionList. You can use that reference as is, so you don't need to use captureQuestionList[${status.index}] to get the object. By the way, the correct syntax for this would be ${captureQuestionList[status.index]}. So, you fieldset name can just be ${captureQuestion.languageId}.

For loops can just be nested. For example (making some assumptions on your question object):

<c:forEach items="${capQues.captureQuestionList}" var="captureQuestion">
  <fieldset name="${captureQuestion.languageId}">
      <legend><c:out value="${captureQuestion.languages}" /></legend>
      <c:forEach items="${captureQuestion.questionList}" var="question">
        <div class="question">
          <textarea class="textarea" name="${question.id}"><c:out
            value="${question.value}"/></textarea>
        </div>
      </c:forEach>
  </fieldset>
</c:forEach>

Note that textarea doesn't have a value attribute. Put the value in it's body.


Edit: If you need to iterate over a list of languages you can use the same principle:

<c:forEach items="${capQues.captureQuestionList}" var="captureQuestion">
  <fieldset name="${captureQuestion.languageId}">
      <legend>
        <c:forEach items="${captureQuestion.languages}" var="language">
          <c:out value="${language.name}" />
        </c:forEach>
      </legend>
      <div class="question">
        <textarea class="textarea" name="${captureQuestion.question}"></textarea>
      </div>
  </fieldset>
</c:forEach>

If you want to display a single language add a c:if to check for the language

<c:forEach items="${captureQuestion.languages}" var="language">
  <c:if test="${language.id eq captureQuestion.questionId}">
    <c:out value="${language.name}" />
  <c:if>
</c:forEach>

Although it would be better to just add a reference to the right language in your model so you can just use ${captureQuestion.language}.

Upvotes: 3

Related Questions