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

タイトルが正しくありません。Collection<State>入力が であると宣言しましたString。Spring は aStateから aを作成する方法を認識できませんでしStringた。それを伝える必要があります。この質問を参照してください: Spring MVC フォーム データ バインディング用の文字列からカスタム オブジェクトへの変換?

于 2012-04-05T14:29:26.307 に答える
0

私も同じ問題を抱えていました。私はSpring、Hibernateを使用しています。複合主キーを持つ 1 つのクラスがあり、リクエストで 2 つのパラメーターを渡します。私の間違いは次のとおりです。

@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 に答える