1

I have a list of question stored in an arraylist questionList and a hashmap whose signature is Map<Integer,List<String>> storing answer for each question . I have mapped integer value with the index value of the question list. Now I want to print each question and its respective answers in a jsp

<c:set var="form" value="${surveyQuestionnaire}"/>
<c:forEach var="questionName" items="${form.questionsList }"   varStatus="theCount">
 Q.<c:out value="${questionName}"></c:out>
 <c:forEach var="questionAnswerMap" items="${form.queChoicesMap }"  >
 </c:forEach>
</c:forEach>

So I am not finding the way to get the value for the given key which would be the index of questionlist and iterate through the list to print all answer

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
abhishek
  • 149
  • 12

1 Answers1

2

Here is demonstration code.

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:set var="questionList" value='${["What color is the sky?","Who invented the telephone?","Which country has the biggest population?"]}'/>
<c:set var="queChoicesMap" value='${{1:["green","brown","blue"],2:["Smith","Bell","Jones"],3:["China","India","USA"]}}'/>
<html>
    <body>
            <c:forEach items="${questionList}" var="question" varStatus="status">
               ${question}
               <c:forEach items="${queChoicesMap[(status.count).longValue()]}" var="choice" varStatus="choiceStatus">
                <br/> ${choiceStatus.count})${choice} 
                </c:forEach> <br/>
            </c:forEach>
    </body> 
</html>

If some of the syntax looks strange to you, then look at http://docs.oracle.com/javaee/7/tutorial/jsf-el004.htm
If you are wondering why the longValue method is there, then look at
EL access a map value by Integer key

Edit: If you read
https://stackoverflow.com/tags/el/info
then you will see that the code above here needs EL version 2.2
To find out which version you are using, read BalusC answer at
How can I check what version of EL is server using
or run the following JSP code.

Server Version: 
${pageContext.servletContext.serverInfo}  
Servlet Version: 
${pageContext.servletContext.majorVersion}.${pageContext.servletContext.minorVersion} 

If you using an older version, then you could use one of the following codes.
But, they have an ugly scriptlet in them. The code in those scriptlets should be moved off into a "tag file" or a "custom EL function".

<%@page import="java.util.*" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
    ArrayList<String> questionList = new ArrayList<String>();
    questionList.add("What color is the sky?");
    questionList.add("Who invented the telephone?");
    questionList.add("Which country has the biggest population?");
    pageContext.setAttribute("questionList", questionList);
    ArrayList<String> listOne = new ArrayList<String>();
    listOne.add("green");
    listOne.add("brown");
    listOne.add("blue");
    ArrayList<String> listTwo = new ArrayList<String>();
    listTwo.add("Smith");
    listTwo.add("Bell");
    listTwo.add("Jones");
    ArrayList<String> listThree = new ArrayList<String>();
    listThree.add("China");
    listThree.add("India");
    listThree.add("U.S.A.");
    HashMap<Integer,List<String>> choicesMap = new HashMap<Integer,List<String>>();
    choicesMap.put(1, listOne);
    choicesMap.put(2, listTwo);
    choicesMap.put(3, listThree);
    pageContext.setAttribute("choicesMap", choicesMap);
%>
<html>
    <body>
            <c:forEach items="${questionList}" var="question" varStatus="status">
                ${question}

                <c:forEach items="${choicesMap[status.count]}" var="choice" varStatus="choiceStatus">
                    <br/> ${choiceStatus.count})${choice} 
                </c:forEach> <br/>
            </c:forEach>
    </body> 
</html>

and

<%@page import="javax.servlet.jsp.jstl.core.LoopTagStatus" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:set var="questionList" value='${["What color is the sky?","Who invented the telephone?","Which country has the biggest population?"]}'/>
<c:set var="choicesMap" value='${{1:["green","brown","blue"],2:["Smith","Bell","Jones"],3:["China","India","U.S.A."]}}'/>
<html>
    <body>
            <c:forEach items="${questionList}" var="question" varStatus="status">
                ${question}
                <%
                    LoopTagStatus status = (LoopTagStatus)pageContext.getAttribute("status");
                    long count = new Integer(status.getCount()).longValue();
                    pageContext.setAttribute("count", count);
                %>
                <c:forEach items="${choicesMap[count]}" var="choice" varStatus="choiceStatus">
                    <br/> ${choiceStatus.count})${choice} 
                </c:forEach> <br/>
            </c:forEach>
    </body> 
</html>
Community
  • 1
  • 1
rickz
  • 4,324
  • 2
  • 19
  • 30
  • this is throwing a syntax error "The function longValue must be used with a prefix when a default namespace is not specified" – abhishek Apr 27 '16 at 09:15
  • Please tell us which server and EL version that you are using and which code worked for you. – rickz Apr 27 '16 at 16:47