I came across to problem with ManyToMany relations between Restaurant and Tag class. I got:
public class Tag {
@Id
private int id;
private String name;
@ManyToMany
@JoinTable(name="restaurant_tag",
joinColumns={@JoinColumn(name="tag_id")},
inverseJoinColumns={@JoinColumn(name="restaurant_id")})
private List<Restaurant> restaurants;
and:
public class Restaurant {
@Id
@GeneratedValue
private int id;
(...)
@ManyToMany
@JoinTable(name="restaurant_tag",
joinColumns={@JoinColumn(name="restaurant_id")},
inverseJoinColumns={@JoinColumn(name="tag_id")})
private List<Tag> tags;
In my jsp I got:
<form:select multiple="true" path="tags">
<form:options items="${tagList }" itemValue="id" itemLabel="name" />
</form:select>
In my controller I got:
public ModelAndView myrestaurantadd(HttpServletRequest request,
HttpServletResponse response, Restaurant restaurant)
throws Exception {
restaurantDAO.saveRestaurant(restaurant);
}
When I click save button, I got error:
Field error in object ‘command’ on field ‘tags’: rejected value [[Ljava.lang.String;@5babd8cb]; codes [typeMismatch.command.tags,typeMismatch.tags,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [command.tags,tags]; arguments []; default message [tags]]; default message [Failed to convert property value of type [java.lang.String[]] to required type [java.util.List] for property ‘tags’; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [beans.Tag] for property ‘tags[0]’: no matching editors or conversion strategy found]] with root cause
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
How are you handling the object at your controller. You must be accepting
java.util.Listin the controller handler method whereas the actual object passed isString[]. Try handling it as String[] and convert it to List in you controller.EDIT:
With regards to your comment, I suspected something of that sort. The tags are passed from jsp in form of String[] tag and you are catching a Restaurant object which would need a List and hence the error. Try the following