Adding a Form Checkbox to a Bound Boolean Field of a List of Objects in JSP (Spring MVC)

In Java web applications built with Spring MVC, it’s common to work with a form that binds to a list of objects, each with a field that should be edited via a checkbox. A classic use case is a list of items where the user can select one or more items by ticking a checkbox, and then submit the form for processing.

This guide walks you through how to bind a checkbox to a boolean field for each object in a list on your form.


Use Case

Let’s say you’re displaying a list of phone number groups (PhoneNumberByAreaCodeSelected), and each has a selected boolean field indicating whether it’s been picked for processing.

You want to:

  1. Display the list in a form.
  2. Show a checkbox for each item that binds to the selected boolean field.
  3. Submit the form and process only the selected items.

Step 1: Define Your Model Classes

PhoneNumberByAreaCode

public class PhoneNumberByAreaCode {
    private String areaCode;
    private String state;
    private BigInteger total;

    // Constructors, getters, setters
}

PhoneNumberByAreaCodeSelected

public class PhoneNumberByAreaCodeSelected extends PhoneNumberByAreaCode {
    private boolean selected;

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

Wrapper List Class

Spring MVC needs a wrapper class around the list to properly bind form fields.

public class PhoneNumberByAreaCodeSelectedList {
    private List<PhoneNumberByAreaCodeSelected> lisst = new ArrayList<>();

    public List<PhoneNumberByAreaCodeSelected> getLisst() {
        return lisst;
    }

    public void setLisst(List<PhoneNumberByAreaCodeSelected> lisst) {
        this.lisst = lisst;
    }
}

Step 2: Spring MVC Controller

GET Method – to render the form

@GetMapping("/list")
public String showForm(Model model) {
    List<PhoneNumberByAreaCodeSelected> data = fetchPhoneNumbers();
    PhoneNumberByAreaCodeSelectedList wrapper = new PhoneNumberByAreaCodeSelectedList();
    wrapper.setLisst(data);

    model.addAttribute("phoneNumberByAreaCodeSelectedList", wrapper);
    return "phone_number_form";
}

POST Method – to process the form

@PostMapping("/list")
public String handleForm(@ModelAttribute PhoneNumberByAreaCodeSelectedList phoneNumberByAreaCodeSelectedList, Model model) {
    List<PhoneNumberByAreaCodeSelected> selectedItems = phoneNumberByAreaCodeSelectedList.getLisst().stream()
        .filter(PhoneNumberByAreaCodeSelected::isSelected)
        .collect(Collectors.toList());

    model.addAttribute("selectedItems", selectedItems);
    return "result_page";
}

Step 3: JSP Form View

Include Spring Form Taglib

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

The Form

<form:form modelAttribute="phoneNumberByAreaCodeSelectedList" method="post">
    <table border="1">
        <thead>
            <tr>
                <th>Select</th>
                <th>Area Code</th>
                <th>State</th>
                <th>Total</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach var="item" items="${phoneNumberByAreaCodeSelectedList.lisst}" varStatus="status">
                <tr>
                    <td>
                        <form:checkbox path="lisst[${status.index}].selected"/>
                    </td>
                    <td>${item.areaCode}</td>
                    <td>${item.state}</td>
                    <td>${item.total}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
    <br/>
    <input type="submit" value="Submit"/>
</form:form>

Explanation:

  • phoneNumberByAreaCodeSelectedList.lisst is the list bound to the form.
  • path="lisst[${status.index}].selected" correctly binds each checkbox to the selected field of the corresponding item.
  • The index ${status.index} ensures the binding maps back to the correct object in the list.

Step 4: Process the Submitted Data

After form submission, the POST method receives the list with updated selected fields. You can process the selected ones:

List<PhoneNumberByAreaCodeSelected> selected = phoneNumberByAreaCodeSelectedList.getLisst().stream()
    .filter(PhoneNumberByAreaCodeSelected::isSelected)
    .collect(Collectors.toList());

Common Issues

“Invalid property ‘items[0]’” error

This usually happens when you mismatch the name of the list in path and the actual getter in the wrapper. Ensure the property name in the path matches the field name exactly (e.g., lisst not items).


Final Notes

  • A checkbox must bind to a boolean property for correct two-way binding.
  • Always use a wrapper class when dealing with a list of objects in Spring forms.
  • The combination of form:checkbox with path="list[index].field" is essential for correct binding.

Wrapping Up

Adding a form checkbox to a bound boolean field of a list of objects in Spring MVC and JSP is simple once you understand how data binding works with lists. The key takeaways are:

  • Use a wrapper object for your list.
  • Match the path correctly in the JSP.
  • Use an indexed approach for binding (list[index].field).

By following this pattern, you can manage complex form inputs in a scalable and maintainable way.

Stay in touch with [US]
If you have any issue you can ask [Here]