• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Displaying multiple checkboxes with the same name.

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a map in the request scope.

Map p = new HashMap();
p.put("1", "name1");
p.put("2", "name2");

and I want to iterate over the map to show a list of checkboxes whose name
will correspond to an indexed property in the form and value will be
"1", "2" etc.. and the labels next to the checkboxes will be name1, name2

<pre>
for (..) {
<input type="checkbox" name="test" value="name<%= i%>">
}
</pre>

How do I do this in Struts using logic:iterate and html:checkbox tags?
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a pretty simple thing to do, but why are you using a map for this?
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.jguru.com/faq/view.jsp?EID=925277


This will help you.
 
author
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would eschew using a map for this, because sometime in the future you may want to have checkboxes mapped to the same value, and it will be very frustrating to manage this. I feel that a better choice would be to create a List of LabelValueBeans, and then iterate through these. If you create the list as some kind of a lookup object then you'll be able to just "grab" the list you need whenever you need it.
 
Danilo Gurovich
author
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the double posting, I hit the reply button too fast. Here's my answer.

Here's a little snippet of code showing <logic:iterate/> tags used to create checkboxes (using a String[] here:

<logic:iterate id="theInstanceOfIteration"
property="theCollectionToIterate"
name="theBeanImGettingItFrom">
<html:multibox property="selectedMountains">
<bean:write name="theInstanceOfIteration"/>
</html:multibox>
<bean:write name="theInstanceOfIteration"/><br/>
</logic:iterate>
 
Alok Pota
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.jguru.com/faq/view.jsp?EID=925277

Did for me.. now using a list of LabelValueBean objects and using

<pre>
<html:multibox>

<logic:notEmpty name="products">
<logic:iterate id="product" name="products">
<html:multibox property="subscribedProduct">
<bean:write name="product" property="value"/>
</html:multibox>
<bean:write name="product" property="label"/> <br>
</logic:iterate>
</logic:notEmpty>
<logic:empty name="products">
There are no products available for selection.
</logic:empty>

</pre>
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic