2012-04-05 61 views
1

我收到此錯誤,當我提交我的表單,無法弄清楚爲什麼會發生這種情況。我相信taglib應該處理這個問題。我試過將jsp中傳遞的值更改爲itemValue="id",但它沒有任何影響。無法修復:'java.lang.String'爲所需的類型'java.util.Collection'

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] 

我的JSP

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

我的內容

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; 
    } 

..... 

我的控制器

... 
@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) { 

... 

我的內容服務

... 
@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(); 
     } 
    } 
} 

... 

回答

0

我有同樣的問題。我使用Spring,Hibernate。 我有一個複合主鍵一個類,並在請求傳遞兩個參數,我的錯誤是:

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

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

控制器:

@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"; 
     } 
    } 

所以......當信息獲取控制器參數clave被視爲主鍵的對象TareaPK

我只是改變了我的控制器中的參數名稱。

@RequestMapping(value = "/tareas", params = {"txt_clave", "tipot"}, method = RequestMethod.GET) 
public String formularioTareaEditar(...){ 
... 
} 
相關問題