Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<logic:iterate> tag 2

Status
Not open for further replies.

huBBLe

Programmer
May 15, 2001
50
0
0
SG
Hi,

I have a Hashtable in the Action class and I did a request.setAttribute("hashtable",ht);

In my target jsp page, I need to iterate through the key-value pairs in the Hashtable and populate a drop downbox. Can the <logic:iterate> tag to do this? I really have no idea how can this be achieved. Please help...

Thanks
 
Ok I managed to figure this out myself. Just in case anyone is interested, here is how to do it:

In the target jsp page, do the following:

Hashtable hashtable = (Hashtable) request.getAttribute("hashtable");
Enumeration enumKeys = hashtable.keys();

<html:select property="someProperty" >
<logic:iterate id="key" type="java.lang.String" collection="<%=enumKeys%>">
<html:eek:ption value="<%=key.toString()%>"><%= hashtable.get(key).toString()%> </html:eek:ption>
</logic:iterate>
</html:select>

If there are any easier and elegant way, feel free to suggest and discuss.

Cheers :)
 
That is great. I was wondering the same thing.

Thanks!
 
Guys, RTFF. Below is a quote from jakarta struts site just about this questuin:


Normally, each object exposed by the iterate tag is an element of the underlying collection you are iterating over. However, if you iterate over a Map, the exposed object is of type Map.Entry that has two properties:
key - The key under which this item is stored in the underlying Map.
value - The value that corresponds to this key.

So, if you wish to iterate over the values of a Hashtable, you would implement code like the following:
<logic:iterate id="element" name="myhashtable">
Next element is <bean:write name="element" property="value"/>
</logic:iterate>
 
Alternatively, you can use <html:eek:ptions> or <html:eek:ptionsCollection>

let say the hashmap contains objects with getter method for lable and value as getLabel() and getValue() respectively:

then you can use optionsCollection tag as:

<html:eek:ptionsCollection
name="hashtable"
value="value"
label="label"
property="someProperty"
/>
 
Here's the way we use the iterate in our code:

Code:
<TABLE BORDER=0> <TR>
   <logic:iterate id="letterval" type="org.bcbsal.indblue.forms.AppInfoLetterRowForm"
                                name="applicationInfoForm" property="applicationLettersList">
  <tr>
   <td><html:link page="/appViewLetter.do"
                          name="letterval"
                          property = "letterParamsToPass">
               <html:img src="./images/ViewLetter.gif" border="0" alt="View Letter"/>
               </html:link>
           </td>
           <td><html:link page="/appEditLetter.do"
                          name="letterval"
                          property = "letterParamsToPass">
               <html:img src="./images/EditLetter.gif" border="0" alt="Edit Letter"/>
               </html:link>
           </td>
           <td><html:link page="/appDeleteLetter.do"
                          name = "letterval"
                          property="letterParamsToPass">
               <html:img src="./images/DeleteLetter.gif" border="0" alt="Delete Letter"/>
               </html:link>
           </td>
           <td><bean:write name="letterval" property="letterTmpltLabel"/></td>
   </tr>
  </logic:iterate>
 </tr>
</TABLE>

Notice the lack of scriptlet:) The letterParamsToPass can contain anything passed from the parent form. You don't have to even think about it. The parent action sets the setter putting the parameters in the HashMap. The jsp doesn't need to know any of this; just give it what it needs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top