0

I have some data to be sent from ModelandView class which has a map. I map the data and send it to my jsp page. But not able to retrieve the data using jstl tag. The controller is sending the data to the specified jsp but its not displayed in the specified field due to jstl tag usage. It would be great if someone finds the error.

Controller.java

    public ModelandView getData(@Context HttpServletRequest request) {
          Map<String,String> map=new HashMap<String,String>();
            ModelandView responseView = new ModelandView("trackData",map);
            //some code here
            if (somecondition) {
                map.put("data",detailsVO.getVehicleId());
                responseView.addAllObjects(map);
            }
            else {
                System.out.println("Not present");
            }
            return responseView;
        }

trackData.jsp

<select class="testdata" id="listvehicle">
<c:forEach var="sample" items="${data}">
<option> ${sample}</option>
</c:forEach>
</select>

The jsp page is not displaying any values in the drop-down list.

Vidya
  • 698
  • 2
  • 12
  • 21

3 Answers3

0

Try accesing key and value. When you say ${sample} it is referring to the entry set of the map. So you need to extract the key and value form the entry. Also you are not setting the varibale and in the for loop trying to access a varible name map. Change that too ModelandView responseView = new ModelandView("trackData", "data", map); and try the below code.

<select class="testdata" id="listvehicle">
    <c:forEach var="sample" items="${data}">
        <option value="${sample.key}"> ${sample.value}</option>
    </c:forEach>
</select>
Syam S
  • 8,421
  • 1
  • 26
  • 36
  • I tried with key and value. Still not getting any results sir – Vidya Jul 04 '14 at 08:28
  • Check that variable name. It should be `items="${trackData}"` and not `items="${map}"` – Syam S Jul 04 '14 at 08:29
  • Its the variable naming. Create the model like `ModelandView responseView = new ModelandView("trackData", "data", map);` and try the above code. The first argument specifies the view name. Second the attribute name and third attribute value. That should fix it.. – Syam S Jul 04 '14 at 08:42
0

You can use a scriptlet in your jsp:

<%@ page import ="java.util.*"%>
<% Map myMap = (Map) request.getAttribute("trackData"); %>

<select class="testdata" id="listvehicle">
<c:forEach var="entry" items="${myMap}">
  <option value="${entry.key}"> ${entry.value}</option>
</c:forEach>
</select>
rob
  • 1,286
  • 11
  • 12
  • If I use a scriplet it gives me the output properly on the console by requesting the attribute but its not updating the drop down list in the jsp page – Vidya Jul 04 '14 at 08:38
  • Edited the answer to fill select box instead of ouputing to page – rob Jul 04 '14 at 09:36
  • You will need to import the required classes in your jsp. Try adding <%@ page import ="java.util.*"%> at the top of your jsp – rob Jul 07 '14 at 08:04
0

you can try like this

<c:forEach var="sampleMap" items="${data}">
Name:  ${sampleMap.key} <br/>
Value: ${sampleMap.value} <br/>
</c:forEach>
learner
  • 365
  • 1
  • 3
  • 16
  • Please explain why this will work to the question author. This does not help show them what was wrong with their original code. Add a description along with your code sample. – Chris Spittles Jul 04 '14 at 09:54