Search code examples
spring-bootjspjstl

How to show set of Arrays in one <TD> in Spring Boot / JSP?


I would like to show all the right answers(Green) in one TD if there are more than one and all the wrong answers(Red) in one TD if there are more than one. Is there anyway in the controller or at the front end that I can achieve this?
With my current code this is what I get.

enter image description here

The aim is to display like this enter image description here

Controller

    @GetMapping("/quesInAss/{id}")
    public String quesInAssessment(@PathVariable("id") Integer id, Model model, HttpSession session) {

        Integer insId = (Integer) session.getAttribute("instId");
        List<Answers> rlist = new ArrayList<Answers>();
        List<Answers> wlist = new ArrayList<Answers>();

        Assessments ass = as.getAllByAssInst(id, insId);
        model.addAttribute("assName", ass.getAssName());

        List<Questions> queslist = qs.getQuesByAssessment(ass);
        for (Questions ques : queslist) {
            List<Answers> anslist = ansService.getAnswersByQuestion(ques);
            for (Answers answer : anslist) {
                if (answer.getAnsStatusCode().equals("CORR")) {
                    rlist.add(answer);
                    Answers[] array = new Answers[rlist.size()];
                    array = rlist.toArray(array);
                    model.addAttribute("rightAnsList", array);                  
                } else {
                    wlist.add(answer);
                    Answers[] array = new Answers[wlist.size()];
                    array = wlist.toArray(array);
                    model.addAttribute("wrongAnsList", array);
                }
            } 
        }
        model.addAttribute("queslist", queslist);
        return "listOfQuesInAss";
    }

JSP

    <c:forEach items="${queslist}" var="ques">
        <tr>
            <td>${ques.quesText}</td>
            
            <c:forEach items="${rightAnsList}" var="rightans">
                <c:if test="${rightans.questions.quesId == ques.quesId}">
                    <td>${rightans.answer}</td>
                </c:if>
            </c:forEach>
            <c:forEach items="${wrongAnsList}" var="wrongans">
                <c:if test="${wrongans.questions.quesId == ques.quesId}">
                    <td>${wrongans.answer}</td>
                </c:if>
            </c:forEach>
        </tr>
    </c:forEach>    

Solution

  • I'm not sure if I could understand your question right, try this:

    <c:forEach items="${queslist}" var="ques">
        <tr>
            <td>${ques.quesText}</td>
             <td>
               <c:forEach items="${rightAnsList}" var="rightans" varStatus="status">
                <c:if test="${rightans.questions.quesId == ques.quesId}">
                   ${rightans.answer}
                </c:if>
                <c:if test="${not status.last}">,</c:if>
               </c:forEach>
            </td>
            <td>
              <c:forEach items="${wrongAnsList}" var="wrongans" varStatus="status">
                <c:if test="${wrongans.questions.quesId == ques.quesId}">
                    ${wrongans.answer}
                </c:if>
                <c:if test="${not status.last}">,</c:if>
              </c:forEach>
            </td>
        </tr>
    </c:forEach>