java – Spring MVC error binding checkboxes to an object list

Question:

I have the following code snippet in my .jsp , where the goal is to dynamically list a passenger list by customer.

<f:form id="service-item-form" action="${action}" modelAttribute="serviceItem" method="post">
   <ul id="list-of-passenger-service-item" class="list-cadastro">
      <c:choose>
         <c:when test="${empty customer.passenger}">
            <li>
               <b>O Cliente não possui passageiros vinculados.</b>
            </li>
         </c:when>
         <c:otherwise>
            <f:checkboxes items="${customer.passenger}" path="passenger" element="li" itemValue="id"/>
         </c:otherwise>
      </c:choose>
   </ul>
</f:form>

Binding is performed in my OpenService.java bean on the passenger attribute, as shown below:

@Entity
@Table(name = "open_service")
public class OpenService implements Serializable{

    //other attributes

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "service_passenger", joinColumns = @JoinColumn(name = "service_id"), inverseJoinColumns = @JoinColumn(name = "passenger_id"))
    private List<Passenger> passenger;

   //Getter and Setter
}

Finally, it triggers an action with the following signature:

@RequestMapping(value = "/add-service/{id}", method = RequestMethod.POST)
public String addService(@ModelAttribute("serviceItem") OpenService openService, @PathVariable Long id) {

   //business rules

   return "redirect:/acme"

}

But the problem is that it always triggers the below conversion error, but as far as I know natively Spring MVC is able to bind with list type objects

Field error in object 'serviceItem' on field 'passenger': rejected value [7]; codes [typeMismatch.serviceItem.passenger,typeMismatch.passenger,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [serviceItem.passenger,passenger]; arguments []; default message [passenger]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'passenger'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [br.com.joocebox.model.Passenger] for property 'passenger[0]': no ​​matching editors or conversion strategy found]

Answer:

Friend, until today I have never seen bind with a list. What you do is bind to an attribute and add that attribute to a list and manipulate it at your leisure.

Attempts to bind a Passenger passenger and add that object to the list and make the necessary modifications.

Scroll to Top