1

One of our application built using Spring MVC + JSP. Please refer below JSP.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.HashMap"%>
<%
    ArrayList<HashMap<String, String>> listOfMap = new ArrayList<HashMap<String, String>>();

    HashMap<String, String> map;

    for (int i = 0; i < 3; i++) {
        map = new HashMap<String, String>();

        map.put("key1", "value1" + i);
        map.put("key2", "value2" + i);

        listOfMap.add(map);
    }

    request.setAttribute("listOfMap", listOfMap);
%>
<html>
<body>
    <c:forEach items="${listOfMap}" var="maps">
        <c:forEach items="${maps}" var="mapItem">
                ${mapItem.key} ${mapItem.value} <br />
        </c:forEach>
    </c:forEach>
</body>
</html>

As you can see in this jsp that i am trying to iterate map and display key and value on page.

But what should be done in case if i want to change the value of mapItem.key and mapItem.value while iterating inside the loop.

So it would like below.

<c:forEach items="${listOfMap}" var="maps">
    <c:forEach items="${maps}" var="mapItem">
            <!--Basically i would write a code (in scriplet) to prevent cross site scripting here -->
            ${mapItem.key} ${mapItem.value} <br />
    </c:forEach>
</c:forEach>
MiniSu
  • 566
  • 1
  • 6
  • 22

0 Answers0