0

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

出现意外错误(类型=内部服务器错误,状态=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 页面中打印 hashmap 值?

4

3 回答 3

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>
于 2018-12-18T09:42:20.990 回答
0

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

<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>

于 2018-12-18T19:30:24.513 回答
0

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

${result.get("res".concat(loop.index)).get(1)}
于 2018-12-19T06:56:19.433 回答