This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Struts and the fly likes Displaying multiple checkboxes with the same name. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Displaying multiple checkboxes with the same name." Watch "Displaying multiple checkboxes with the same name." New topic
Author

Displaying multiple checkboxes with the same name.

Alok Pota
Ranch Hand

Joined: Mar 07, 2001
Posts: 185
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

Joined: Feb 05, 2003
Posts: 4727

This is a pretty simple thing to do, but why are you using a map for this?


A good workman is known by his tools.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
http://www.jguru.com/faq/view.jsp?EID=925277


This will help you.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Danilo Gurovich
author
Greenhorn

Joined: Aug 08, 2002
Posts: 7
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<br />Manager, Web Development<br /> <br />dgurovich@lowermybills.com<br />2401 Colorado Ave., 2nd Floor <br />Santa Monica, CA 90404<br />(310) 998-6412
Danilo Gurovich
author
Greenhorn

Joined: Aug 08, 2002
Posts: 7
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

Joined: Mar 07, 2001
Posts: 185
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>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying multiple checkboxes with the same name.
 
Similar Threads
IMP c:remove doubt
problem in getInitParameterNames() method of ServletConfig
XPath select
resultset return null
jstl + forEach + x number of items per row