在 actionform 中定义一个数组 string[] selectedoptions 用于存放复选框被选中的值,
数组 labelvaluebean[] possibleoptions 用于显示所有的复选框的值。
import org.apache.struts.util.labelvaluebean; . . . public class myactionform extends actionform { private labelvaluebean[] possibleoptions; private string[] selectedoptions; public myactionform() { // initialise the labelvaluebeans in the possibleoptions array. labelvaluebean[] lvbeans = new labelvaluebean[4]; lvbeans[0] = new labelvaluebean("alpha", "a"); lvbeans[1] = new labelvaluebean("beta", "b"); lvbeans[2] = new labelvaluebean("charlie", "c"); lvbeans[3] = new labelvaluebean("delta", "d"); this.possibleoptions = lvbeans; } public labelvaluebean[] getpossibleoptions() { return possibleoptions; } public string[] getselectedoptions() { return selectedoptions; } public void setselectedoptions(string[] selectedoptions) { this.selectedoptions = selectedoptions; } }
在 jsp 中使用以下代码:
<logic:iterate name="myactionform" id="item" property="possibleoptions"> <html:multibox property="selectedoptions"> <bean:write name="item" property="value" /> </html:multibox> <bean:write name="item" property="label" /><br /> </logic:iterate>
