Iterate through HashMap using Struts2 Iterator

I am pretty much fond of using JSTL also called as JavaServer Pages Standard Tag Library and tag libraries inside JSP code, the only reason i see it using is to get rid of the JSP scriptlet code written in between HTML codes. But i was stuck at one point when i wanted to iterate through HashMap using Struts Iterator. After much googling i found the solution and in this article we would understand on how to iterate through HashMap using Struts Iterator.

Assume we want to populate the SELECT box with the content of HashMap, we would write the below code.

Now we would try to understand the above code.

selectData – This is the HashMap object that you have received from Struts Action class.
data – This is the name that would be used to reference the key and value pushed into the HashMap

To read the key from HashMap we would read the key property
To read the value from HashMap we would read the value property

There are scenarios where in we want to perform conditional check for key while iterating through the HashMap. Remember we discussed about ‘data’ variable which can be used as reference to read the key and value in HashMap. We would be using that and code would be written as.

The above code would check for key having data = 2 or not.

Tagged . Bookmark the permalink.

Leave a Reply