在JSP页面中打印哈希图值

时间:2018-12-18 09:36:55

标签: java spring jsp hashmap

我正在尝试在jsp页面中调用一个HashMap值,但那里出现错误并且错误提示

  

发生意外错误(类型=内部服务器错误,状态= 500)。   对于输入字符串:“ res”

我的代码;

<c:forEach items="${onlineExamList}" var="item"
                                        varStatus="loop">

                                        <div>
                                            <b>${item.question1}</b><br>

                                            <div class="radio">
                                                <label><input type="radio" value="a"
                                                    name="answers[${loop.index}]">${item.option1}</label>
                                            </div>
                                            <div class="radio">
                                                <label><input type="radio" value="b"
                                                    name="answers[${loop.index}]">${item.option2}</label>
                                            </div>
                                            <div class="radio">
                                                <label><input type="radio" value="c"
                                                    name="answers[${loop.index}]">${item.option3}</label>
                                            </div>
                                            <div class="radio">
                                                <label><input type="radio" value="d"
                                                    name="answers[${loop.index}]">${item.option4}</label>
                                            </div>
                                            <input type="text" name="rightAnswer"
                                                value="${item.rightAnswer}">



//problem in this line
                                            <c:if test="${result != null}">
                                                <br>
                                                <br>
                                                <b>Your answer: ${result.get("res"+loop.index).get(1)}</b>

                                                <br>
                                            </c:if>
                                        </div>

                                        <hr />
                                    </c:forEach>

这就是我从控制器设置哈希图的方式

Map<String, List<String>> mapResult = new HashMap<String, List<String>>();

        int totalScore = 0;

        for (int i = 0; i < answers.answers.size(); i++) {
            List<String> result = new ArrayList<>();
            String res = "Wrong";
            if (answers.answers.get(i).equals(answers.rightAnswer.get(i))) {
                res = "Correct";
                totalScore+=10;
            }

            result.add(res);
            result.add(answers.answers.get(i));
            result.add(answers.rightAnswer.get(i));

            mapResult.put("res" + i, result);
        }

        ra.addFlashAttribute("result", mapResult);
        ra.addFlashAttribute("score", totalScore);

java页面上印有相同内容

for (int i = 0; i < 10; i++) {
            System.out.println(mapResult.get("res" + i).get(0));
            System.out.println(mapResult.get("res" + i).get(1));
            System.out.println(mapResult.get("res" + i).get(2));
            System.out.println("...................................");;
        }

如何在jsp页面中打印哈希图值?

3 个答案:

答案 0 :(得分:0)

请检查尝试。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:forEach items="${currentLoggedInUsersMap}" var="entry">
    Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>

答案 1 :(得分:0)

mapResult对象是包含字符串数组的映射
Hashmap<String, List<String>>
您可能需要先从地图对象获取Array,然后对其进行迭代。
这可能有帮助吗?

<table> <c:forEach var="results" items="${resultsMap['res' + loop.index]}" varStatus="loop"> <tr> <c:forEach var="answer" items="${results}" varStatus="status"> <td> <td>$answer</td> </td> </c:forEach> </tr>
</c:forEach>
</table>

答案 2 :(得分:0)

我通过使用concat这样的字符串来解决问题

${result.get("res".concat(loop.index)).get(1)}