我有一个问题列表存储在一个数组列表问题列表中,还有一个哈希图,其签名Map<Integer,List<String>>
存储每个问题的答案。我已经将整数值与问题列表的索引值映射。现在我想在 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>
所以我没有找到获取给定键的值的方法,该键将是问题列表的索引并遍历列表以打印所有答案
这是演示代码。
<%@ 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>
如果某些语法对您来说看起来很奇怪,请查看
http://docs.oracle.com/javaee/7/tutorial/jsf-el004.htm如果你想知道为什么长值方法在那里,那么看看
EL 通过整数键访问映射值
编辑:如果您阅读
https://stackoverflow.com/tags/el/info那么你会看到上面的代码需要 EL 版本 2.2
要了解您使用的是哪个版本,请阅读 BalusC 答案
如何检查服务器使用的
EL 版本或运行以下 JSP 代码。
Server Version:
${pageContext.servletContext.serverInfo}
Servlet Version:
${pageContext.servletContext.majorVersion}.${pageContext.servletContext.minorVersion}
如果您使用的是旧版本,则可以使用以下代码之一。
但是,他们有一个丑陋的脚本。这些脚本中的代码应移至"标记文件"或"自定义 EL 函数"中。
<%@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>