How to Make Mailbox Style Multiple Checkboxes and Pre-select

Form bean:

public class MyForm {
  private String[] selectedIds;

  public String[] getSelectedIds() {
    return this.selectedIds;
  }
  public void setSelectedIds(String[] ids) {
    this.selectedIds = ids;
  }
}

Action:

public class MyAction extends ActionSupport {
  MyForm form;
  List<String> completeList;

  public String getForm() {
    return this.form;
  }

  public List<String> getCompleteList() {
    return this.completeList;
  }
}

JSP:

<s:iterator value="completeList" var="id">
<tr>
  <td><s:checkbox name="form.selectedIds" fieldValue="%{id}" id="id-%{id}" value="%{id in form.selectedIds}" /></td>
</tr>
</s:iterator>

How to Create a Struts 2 Component with Freemarker

For quick starter, you can use tag file to create a reusable component for your JSP application. However, if you wants a more serious tag, or need to use Struts’ value stack ability to lookup action variable, it is better to create  a Struts component.

The steps are simple:

That’s all! This is tested on Struts 2.3.x. Continue reading