Uncommented Bytes

Friday, April 08, 2005

Using Struts multibox and radio tags

I've found a great way to use both the Struts multibox and radio tags, and just want to document it here for my future use.

The multibox is a tag to output multiple linked checkboxes, so the user can select multiple items at once. The radio is the same idea, but for a single select.

My jsp piece looks like this, and it generates a table with a radio button as the first field:

<c:forEach var="data" items="${MyForm.dataList}">
<td><html:radio property="selectedData" value="${data.id}"/></td>
<td><c:out value="${data.name}"/></td>
<td><c:out value="${data.location}"/></td>

Now, you can change it to a multiselect version using the multibox by replacing the html:radio tag with this one:
<html:multibox property="selectedData" value="${data.id}"/>

In my MyForm, I have a public List getDataList() and a public String getSelectedData() for the single select with the radio button. For the multiselect with the multibox you have to use public String[] getSelectedData() instead. (of course, both need the corresponding setSelectedData() method...)

[edited to remove confusing custom rowColor tag, 2/20/2006]


  • Its a neat help. But put also java components to make it more easy to comprehend.

    By Anonymous Rajesh, at 14 July, 2006 08:37  

  • can i use this if i want to I want to maintian a hidden fields for all checked values for the multibox. Because when I come back from the other jsp pages, checked vauels of html:multibox are getting disappear.

    By Anonymous macpherson struts, at 05 August, 2008 17:54  

  • macpherson - Sounds like you haven't implemented the getDataList correctly...

    By Blogger Jeff Sheets, at 05 August, 2008 19:15  

Post a Comment

<< Home