簡體   English   中英

在JSP頁面中打印哈希圖值

[英]Print hashmap value in jsp page

我正在嘗試在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頁面中打印hashmap值?

請檢查嘗試。

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

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>

我通過像這樣使用concat來壓縮字符串來解決問題

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM