You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Tim Colson <tc...@cisco.com> on 2003/02/27 21:59:24 UTC
RE: order of the insertion - works for me
> Is it possible to retain the order in which elements where
> inserted into a map while this
> map is being rendered by Velocity using #foreach?
I was about to jump in and say "no" as maps don't have order....but then
read a little further....
> I'm using LinkedHashMap when building the context,
Seems LinkedHashMap is new in JDK 1.4 and is supposed to maintain the
order of insertion.
http://java.sun.com/j2se/1.4/docs/api/java/util/LinkedHashMap.html
Cool and Interesting.
I tried it out and it worked fine for me. :-)
Code/template/output below, using
Velocity 1.3 released
JSDK 1.4.1 on Winbloze XP
and the VelocityViewServlet.
Cheers,
Timo
// TESTING CODE
ArrayList branchData = new ArrayList();
LinkedHashMap row = null;
row = new LinkedHashMap();
row.put("loan_number","123"); //column 1
row.put("borrower_first_name", "Tim");//column 2
row.put("borrower_last_name", "Colson");//column 3
branchData.add(row); //add row to array list
row = new LinkedHashMap();
row.put("loan_number","456"); //column 1
row.put("borrower_first_name", "Joe");//column 2
row.put("borrower_last_name", "Normal");//column 3
branchData.add(row); //add row to array list
row = new LinkedHashMap();
row.put("borrower_last_name", "Backward");//column 1
row.put("borrower_first_name", "Joe");//column 2
row.put("loan_number","789"); //column 3
branchData.add(row); //add row to array list
request.setAttribute("branch_list", branchData);
// END TEST
--- template.vm
TESTING
<PRE>
#foreach ($rowhash in $branch_list)
#foreach ($item in $rowhash)$item #end
#end
</PRE>
---- OUTPUT
TESTING
123 Tim Colson
456 Joe Normal
Backward Joe 789
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org