Saturday, March 15, 2008

java: How to put checkboxes in DisplayTag using Struts multibox


<display:table name="sessionScope.myFormBean.mySearchResultArrayList" id="myRecord">
<display:column title="Select">
<html:multibox property="selectedItems"
value='<%= (String)((com.abc.def.MyClass)myRecord).getAbc().toString() %>' />
</display:column>
</display:table>


Note:

myFormBean is the name of your Form Bean (i.e. MyFormBean.java)

mySearchResultArrayList is a collection getter in your MyFormBeanName.java. It will hold a collection of your MyClass.java

MyClass will represent each row in your DisplayTag (i.e. MyClass.java). Your mySearchResultArrayList will contain many MyClass.java

myRecord represents the reference to the MyClass object.

selectedItems is a String array property in your MyFormBeanName.java with a getter/setter. It will hold the values of the selected checkboxes. It will also be the name of the checkbox group in html.


MyClass.java


public class MyClass
{
private String abc;

public void setAbc(String abc)
{
this.abc = abc;
}

private String getAbc( )
{
return abc;
}
}



MyFormBean.java

public class MyFormBean
{
private String[ ] selectedItems;
private List mySearchResultArrayList;

public void setMySearchResultArrayList(List list)
{
mySearchResultArrayList = list;
String[] selectedItems = (String[]) mySearchResultArrayList.toArray(new String[0]);
}

public List get
MySearchResultArrayList( )
{
return
mySearchResultArrayList;
}

public String[ } getSelectedItems( )
{
return
selectedItems;
}

public void setSelectedItems(String[ ] selectedItems)
{
this.selectedItems = selectedItems;
}
}

No comments: