java - How to iterate an ArrayList inside a HashMap using JSTL? -
I have a map like this,
map & lt; Integer, Arrayist & lt; Object & gt; & Gt; ; MyMap = New LinkedHash & lt; Integer, Arrayist & lt; Object & gt; & Gt; ();
Now I have to do this map again and then inside the map ArrayList. How can I do this by using JSTL?
You can use tags to arrays, archive and recycle on the map.
In the case of arrays and collections, running var
will promptly present you the current item.
& lt;% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" % & Gt; & Lt; C: forEach item = "$ {collectionOrArray}" var = "item" & gt; Item = $ {item} & lt; Br> & Lt; / C: foreach & gt;
In the case of a map, every frequency will give you an object which in turn getKey ()
and GetValue ()
methods.
and lt;% @ taglib prefix = "c" uri = "http: //java.sun com / jsp / jstl / core" & Gt; & Lt; C: forEach item = "$ {map}" var = "entry" & gt; Key = $ {entry.key}, value = $ {entry.value} & lt; Br> & Lt; / C: foreach & gt;
In your special case, $ {entry.value}
is actually a list
, so you need to it iterate Well:
& lt;% @ taglib prefix = "c" yurry = "http://java.sun.com/jsp / Jstl / core "% & Gt; & Lt; C: forEach item = "$ {map}" var = "entry" & gt; Key = $ {entry.key}, value = & lt; C: for each item = "$ {entry.value}" var = "item" varStatus = "loop" & gt; $ {Item} $ {! Loop.last? ',': ''} & Lt; / C: forEach & gt; & Lt; Br> & Lt; / C: foreach & gt;
varStatus
is for convenience only;)
To understand what's going on here, here's a plain Java The translation is: <(entry
.getKey () + ", value ="); (Iterator> Lieutenant; Object> gt; iter = entry.getValue (). Iterator (); iter.hasNext ();) {object object = iter.next (); outside. Print (item + (iter.hasNext ()? ",": "" ""))}} Out.println ();}
See also:
Comments
Post a Comment