1

I'm getting this error when I submit my form and cannot figure out why this is happening. I believe the taglib should be handling this. I've tried changing the value passed in my jsp to itemValue="id" but it has no affect.

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'content' on field 'stateCollection': rejected value [com.myapp.cmt.model.State[ id=3 ]]; codes [typeMismatch.content.stateCollection,typeMismatch.stateCollection,typeMismatch.java.util.Collection,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [content.stateCollection,stateCollection]; arguments []; default message [stateCollection]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Collection' for property 'stateCollection'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type [java.lang.String] to required type [com.myapp.cmt.model.State] for property 'stateCollection[0]': no matching editors or conversion strategy found]

My jsp

<strong>State</strong><br/>
<form:checkboxes path="stateCollection" items="${states}" itemLabel="name"/>

My Content

public class Content implements Serializable {
.......

    @JoinTable(name = "content_to_state", joinColumns = {
        @JoinColumn(name = "content_id", referencedColumnName = "id")}, inverseJoinColumns = {
        @JoinColumn(name = "state_id", referencedColumnName = "id")})
    @ManyToMany
    private Collection<State> stateCollection;

.....

    @XmlTransient
    public Collection<State> getStateCollection() {
        return stateCollection;
    }

    public void setStateCollection(Collection<State> stateCollection) {
        this.stateCollection = stateCollection;
    }

.....

My Controller

...
@RequestMapping(value = "/{guid}/save", method = RequestMethod.POST)
public ModelAndView saveContent(@ModelAttribute("content") Content content, @PathVariable("guid") String guid) {
    try {
        // Save the modified object
        contentService.save(content);
    } catch (IllegalOrphanException ex) {

...

My content service

...
@Transactional
public void save(Content content) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
    try {
        utx.begin();
        em.merge(content);

        utx.commit();
    } catch (Exception ex) {

    } finally {
        if (em != null) {
            em.close();
        }
    }
}

...
Ben
  • 60,438
  • 111
  • 314
  • 488

2 Answers2

0

Your title isn't correct. You have declared a Collection<State> your input is a String. Spring couldn't know how to make a State from a String, you have to tell it. Please see this question: Converting from String to custom Object for Spring MVC form Data binding?

Community
  • 1
  • 1
Kai
  • 38,985
  • 14
  • 88
  • 103
0

I had the same problem. i'm using Spring, Hibernate. I have one class with composite primary key and pass two parameters in request, my mistake was:

@Entity
@Table(name = "TAREAS")
public class Tarea implements Serializable {

   private static final long serialVersionUID = 1L;
   protected TareaPK clave;
   private String descripcion;
   .....
}

the controller:

   @RequestMapping(value = "/tareas", params = {"clave", "tipot"}, method = RequestMethod.GET)
   public String formularioTareaEditar(
       @RequestParam(value = "clave") String clave,
       @RequestParam(value = "tipot") String tipoTrabajo,
       Model model) {
     Tarea tarea = catalogoService.getTarea(tipoTrabajo, clave);
     model.addAttribute(tarea);
     return "tarea/editar";
   }

   @RequestMapping(value = "/tareas", params = {"clave", "tipot"}, method = RequestMethod.POST)
   public String tareaEditar(@Valid @ModelAttribute Tarea tarea, BindingResult result) {
      if (result.hasErrors()) {
         return "tarea/editar";
      } else {
         catalogoService.edit(tarea);
         return "redirect:/tareas";
      }
   }

So... when the info gets in the controller the parameter clave is considered as if the object TareaPK of the primary key.

i just change the name of the parameter in my controller.

@RequestMapping(value = "/tareas", params = {"txt_clave", "tipot"}, method = RequestMethod.GET)
public String formularioTareaEditar(...){
...
}
jmct_x
  • 33
  • 5