Spring MVC error in performing binding of checkboxes in a list of objects

3

I have the following code snippet in my .jsp , in which the purpose is to dynamically list a passenger list per 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>

bind is carried out in my bean OpenService.java in 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 error below conversion, but as far as I know natively Spring MVC is able to bind with list 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:   Can not convert value of type [java.lang.String] to required type   [br.joocebox.model.Passenger] for property 'passenger [0]': no   matching editors or conversion strategy found]

    
asked by anonymous 20.11.2015 / 14:26

1 answer

0

Friend, to this day I've never seen bind with list. What you do is bind in an attribute and add that attribute to a list and manipulate it as you wish.

Try to bind to a Passenger passenger and add this object to the list and make the necessary modifications.

    
22.11.2015 / 15:29