【问题标题】:to display a value of nested list in a list on jsp在jsp的列表中显示嵌套列表的值
【发布时间】:2012-10-05 12:54:38
【问题描述】:

我正在尝试使用 c:forEach syantax 在 jsp 上迭代 pojos 列表。 现在的问题是列表包含一个嵌套列表,那么我应该如何在 jsp 上显示该特定值。

这是我在 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>

其中语言也是captureQuestionList中的一个列表。

提前致谢

【问题讨论】:

    标签: jsp spring-mvc foreach jstl


    【解决方案1】:

    我认为您在这里缺少的是var 的要点。在您的第一个循环中,captureQuestion 将是来自列表captureQuestionList 的当前对象。您可以按原样使用该引用,因此无需使用captureQuestionList[${status.index}] 来获取对象。顺便说一句,正确的语法是${captureQuestionList[status.index]}。因此,您的字段集名称可以只是 ${captureQuestion.languageId}

    For 循环只能嵌套。例如(对您的问题对象做出一些假设):

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

    请注意,textarea 没有 value 属性。将值放入它的主体中。


    编辑:如果您需要遍历语言列表,您可以使用相同的原则:

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

    如果您想显示一种语言,请添加 c:if 以检查语言

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

    虽然最好在模型中添加对正确语言的引用,这样您就可以使用${captureQuestion.language}

    【讨论】:

    • 嗨 Japser,感谢您的回复,但我面临的问题是在 标记中,我想显示一种语言的名称,该名称在 captureQuestion 中名为语言的列表中...在简而言之,我的意思是 captureQuestion 包含语言列表,语言列表包含语言名称,,,那么我应该如何打印语言名称。我们将不胜感激。
    • 您好 Japser 非常感谢您的及时回复。正如您所说,它工作正常。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    相关资源
    最近更新 更多