Saturday, February 26, 2011

Reading data from Hashmap using JSTL

The requirement was to retrieve date based on the key from a HashMap and to display it in the UI.This is a common scenario when using the foreign key data.Initially seemed quite straightforward to create a HashMap to store the key and Values.In the JSP front ,the data can be retrieved either by
(i) looping the contents ( construct ) to identify the match or
(ii) retrieve the data from HashMap using the key( map["key"]).
I preferred the later approach since it was more performance effective.

Map map = new HashMap();
map.put(1, "Monday");
map.put(2, "Tuesday");
map.put(3, "Wednesday");

This was not rendering the data in the page.But when looping the contents the match was found and displayed in the page.

By modifying the key type in HashMap to String the values were retrieved and displayed in the Page.
Map map = new HashMap();
map.put("1", "Monday");
map.put("2", "Tuesday");
map.put("3", "Wednesday");

My inference from this is that EL does not support the Interger wrapper class for HashMap.But I could not find any documentation to confirm this.