0

I have 2 set of data in my jsp page , one is a array list and another is json data . Now i want to parse both the data set and create my own drop downs .

Here is the sample Array List named json_obj data

["ABC-1","ABC-2","ABC-3","ABC-4","ABC-5","ABC-6"]

I tried this piece of code but not working

<select>
  <option value="all_qns">All</option>
  <c:forEach var="strategy" items="${json_obj}" varStatus="strategyLoop">
    <option><c:out value="${strategyLoop[index]}"/></option>

  </c:forEach>
</select>

Getting blank options

<select>
 <option value="all_qns">All</option>
 <option></option>
 <option></option>
 <option></option>
 <option></option>
 <option></option>
 <option></option>
</select>

Also I do have this piece of json data named json_obj_m

{"a":"1050","b":"1079","c":"1073","d":"1074"}

And I a=have tried this :

<c:forEach items="${json_obj_m}" var="met">
 <option value="${met.key}">${met.value}</option>
</c:forEach>

But not working again getting error , that spring does not support key .

Can anybody guide me where I am doing mistake, very new to Java/Spring . Thanks in advance.

curiousguy
  • 2,980
  • 7
  • 34
  • 62

2 Answers2

1

Your usage of JSTP foreach is incorrect : you get the value in strategy and try to use (badly) strategyLoop which is the status. You should write simply :

<option>${strategy}</option>

The status helps to count the iterations and you use ${strategyLoop.index} or ${strategyLoop.count}:

  • strategyLoop.index starts at 0
  • strategyLoop.count starts at 1
Serge Ballesta
  • 136,215
  • 10
  • 111
  • 230
1

For iterating over list use this code:

<select id="someId">
  <option value="all_qns">All</option>
  <c:forEach var="strategy" items="${json_obj}" >
    <option value="${strategy}">${strategy}</option>

  </c:forEach>
</select>

And if you are getting JSON through Ajax call, you can use this (By JavaScript):

$.each(data, function(key, value) { 
         $('#someId').append("<option value="+key+"option>"+value+"</option>");  
    });
});
Shailesh Saxena
  • 3,412
  • 2
  • 17
  • 28
  • `JSON` data works fine , But when I am doing the same for `ArrayList` it is not working . I made it working by `Javascript` as : `dataMatel = ; $.each(dataMatel, function(value) { $('#questionID').append(""); });` But still I am not sure how to iterate in JSP page(not using Javascript) . I tried this `

    ${strategy}

    ` But Output is like `["ABC-1" "ABC-2" "ABC-3"
    – curiousguy Jul 17 '14 at 06:54