1

提交表单时出现此错误,但无法弄清楚为什么会发生这种情况。我相信 taglib 应该处理这个问题。我尝试将在我的 jsp 中传递的值更改为,itemValue="id"但它没有任何影响。

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

...
4

2 回答 2

0

你的标题不正确。您已声明 aCollection<State>您的输入是 a String。Spring 不知道如何State从 a 中生成 a String,你必须告诉它。请看这个问题:Converting from String to custom Object for Spring MVC form Data binding?

于 2012-04-05T14:29:26.307 回答
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(...){
...
}
于 2013-12-26T05:46:44.053 回答