HTML - checkbox conditionally checked

html, java, jsp, spring, spring-mvc

Solution

You can use `EL` ternary operator like this:

 <input type="checkbox"  ${person.adult ? 'checked' : ''}>

`${person.adult}` will invoke `isAdult` method that encapsulating `adult` attribute.

Problem

I have written this simple jsp page: I want to show a simple list of object (Person), with a label (showing the name of the person) and a checkbox (that shows if the person is at least 18 yo). I also write the corresponding Java class 'Person', with a String (name) and a boolean (isAdult). Here is my jsp (personList is a List of Person objects) ``` <table> <th>NAME</th> <th>IS ADULT</th> <c:forEach var="person" items="${personList}"> <tr> <td>${person.name}</td> <td> <input type="checkbox" value="checked"/> </td> </tr> </c:forEach> </table> ``` This portion of code shows me a list in which all checkboxes are not checked (names are correct. How could I obtain that every checkbox in the list is checked (or not), referring to the boolean attribute 'isAdult' of the Person object?

Original source