简体   繁体   中英

Trying to implement jstl in jsp from List

Here is my Java code :

   List<EmployeeBean>questionList=adao.displayQuestions(eBean,functional_id);
   model.addObject("questionList", questionList);

   Iterator<EmployeeBean> iterator = questionList.iterator();
    while (iterator.hasNext()) {
       String q_id =iterator.next().getSk_question_id();
       System.out.println("QUESTION ID :"+q_id);
       List<EmployeeBean>optionList= adao.displayOptions(eBean,q_id);
       model.addObject("optionList", optionList);
      }

String q_id will have mutiple id's based on the first list (questionList). Based on q_id, I will get multiple options from second list (optionList).

Here is what I am trying in jsp: I am not able to get optionList values. getting only last values of the list

  <c:forEach var="EmployeeBean" items="${questionList}">
   ${EmployeeBean.question_name }

     <c:forEach var="EmployeeBean" items="${optionList}">
       <div class="col-xs-4 starategy-data">
       <div class="strategy-head">${EmployeeBean.answer_type }</div>
       <p>${EmployeeBean.answer }</p>
       </div>
     </c:forEach>
  </c:forEach>

One possible way(it may not show correct,if one q_id not has related data list):

   List<EmployeeBean>questionList=adao.displayQuestions(eBean,functional_id);
   model.addObject("questionList", questionList);

   List<List<EmployeeBean>> resultList = new ArrayList<>();
   Iterator<EmployeeBean> iterator = questionList.iterator();
   while (iterator.hasNext()) {
          String q_id = iterator.next().getSk_question_id();
          System.out.println("QUESTION ID :"+q_id);
          resultList.add(adao.displayOptions(eBean,q_id));//store it by index
   }
   model.addObject("optionList", resultList);

JSP code:

 <c:forEach var="EmployeeBean" items="${questionList}" varStatus="status">
   ${EmployeeBean.question_name }

     <c:forEach var="EmployeeBean" items="${optionList.get(status.index)}"><!-- get the related data list -->
       <div class="col-xs-4 starategy-data">
       <div class="strategy-head">${EmployeeBean.answer_type }</div>
       <p>${EmployeeBean.answer }</p>
       </div>
     </c:forEach>
  </c:forEach>

Another way(I have not try,you can test)

Java code:

   List<EmployeeBean>questionList=adao.displayQuestions(eBean,functional_id);
   model.addObject("questionList", questionList);

   int index = 0;
   while (iterator.hasNext()) {
          String q_id = iterator.next().getSk_question_id();
          System.out.println("QUESTION ID :"+q_id);
          model.addObject("optionList_"+index,adao.displayOptions(eBean,q_id));
          index++;
   }

JSP code:

<c:forEach var="EmployeeBean" items="${questionList}" varStatus="status">
   ${EmployeeBean.question_name }

     <c:forEach var="EmployeeBean" items="${optionList_status.index}"><!-- not sure if it can work correct here -->
       <div class="col-xs-4 starategy-data">
       <div class="strategy-head">${EmployeeBean.answer_type }</div>
       <p>${EmployeeBean.answer }</p>
       </div>
     </c:forEach>
  </c:forEach>

Try this:

   List<EmployeeBean>questionList=adao.displayQuestions(eBean,functional_id);
   model.addObject("questionList", questionList);


   List<EmployeeBean>optionList = new ArrayList<>();
   Iterator<EmployeeBean> iterator = questionList.iterator();
   while (iterator.hasNext()) {
          String q_id =iterator.next().getSk_question_id();
          System.out.println("QUESTION ID :"+q_id);
          optionList.addAll(adao.displayOptions(eBean,q_id)); 
   }
   model.addObject("optionList", optionList);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM