Jsp 如何根据jstl中另一个lis的索引值迭代hashmap
我有一个存储在arraylist问题列表中的问题列表和一个hashmap,其签名是Jsp 如何根据jstl中另一个lis的索引值迭代hashmap,jsp,jstl,Jsp,Jstl,我有一个存储在arraylist问题列表中的问题列表和一个hashmap,其签名是Map存储每个问题的答案。我已将整数值与问题列表的索引值映射。现在我想在jsp中打印每个问题及其相应的答案 <c:set var="form" value="${surveyQuestionnaire}"/> <c:forEach var="questionName" items="${form.questionsList }" varStatus="theCount"> Q.<
Map
存储每个问题的答案。我已将整数值与问题列表的索引值映射。现在我想在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>
Q
因此,我没有找到获取给定键的值的方法,该键将是问题列表的索引,并在列表中迭代以打印所有答案,这里是演示代码
<%@ 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>
如果您使用的是旧版本,则可以使用以下代码之一。但是,他们有一个丑陋的脚本。这些scriptlet中的代码应该转移到“标记文件”或“自定义EL函数”中
${问题}
${choiceStatus.count})${choice}
及
${问题}
${choiceStatus.count})${choice}
这引发了一个语法错误“当未指定默认名称空间时,函数longValue必须与前缀一起使用”请告诉我们您正在使用的服务器和EL版本以及适用于您的代码。
Server Version:
${pageContext.servletContext.serverInfo}
Servlet Version:
${pageContext.servletContext.majorVersion}.${pageContext.servletContext.minorVersion}
<%@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>
<%@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>