• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying multiple checkboxes with the same name.

 
Alok Pota
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • 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?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a pretty simple thing to do, but why are you using a map for this?
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.jguru.com/faq/view.jsp?EID=925277


This will help you.
 
Danilo Gurovich
author
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
  • 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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic