2017-01-12 52 views
0

私はSpring Controller(provincia)からのコンテンツをリストに入れました。選択した値を取得するために必要な別のオブジェクト(profile2)があります。 (provincia)...と私のフォーム国の他の値、等...私のHTMLでThymeleafとSpringブートでドロップダウンリストから選択した値を抽出する方法

@RequestMapping(value = "/editprofileabout4", method = RequestMethod.GET) 
public ModelAndView editProfileAbout4(ModelAndView modelAndView) { 

    Usuario usuario = getUsuario(); 
    Profile profile = profileService.getUserProfile(usuario); 
    Profile2 profile2 = profile2Service.getUserProfile2(usuario); 
    List<Provincia> provincia = provinciaService.readAllProvincia(); 

    Profile webProfile = new Profile(); 
    webProfile.safeCopyFrom(profile); 

    modelAndView.getModel().put("edituserprofile2th", usuario); 
    modelAndView.getModel().put("editprofile2about2th", profile2); 
    modelAndView.getModel().put("editprovincia2th", provincia); 
    modelAndView.setViewName("editprofileabout4"); 

    return modelAndView; 
} 

@RequestMapping(value = "/doeditprofileabout4", method = RequestMethod.POST) 
public ModelAndView editProfileAbout4(ModelAndView modelAndView, @Valid Profile2 profile2, @Valid Provincia provincia, BindingResult result) { 

    modelAndView.setViewName("editprofileabout4"); 

    System.out.println("  !!!!!!!!!!!!! PROFILE2a: " + profile2.getBirthdate()); 
    System.out.println("  !!!!!!!!!!!!! PROFILE2b: " + profile2.getGender()); 
    System.out.println("  !!!!!!!!!!!!! PROFILE2d: " + profile2.getProvincia()); 
    System.out.println("  !!!!!!!!!!!!! PROFILE2x: " + provincia.toString()); 

    Usuario usuario = getUsuario(); 
    Profile2 profile = profile2Service.getUserProfile2(usuario); 
    profile.setCountry(profile2.getCountry()); 
    profile.setGender(profile2.getGender()); 
    profile.setBirthdate(profile2.getBirthdate()); 
    profile.setProvincia(provincia); 

    if (!result.hasErrors()) { 
     profile2Service.save(profile); 
     modelAndView.setViewName("redirect:/editprofileabout4"); 
    } 
    return modelAndView; 

} 

、私が持っている:

<form class="sky-form" id="sky-form4" action="#" th:object="${editprofile2about2th}" th:action="@{/doeditprofileabout4}" method="post"> 

    <section> 
    <div class="col-lg-3"> 
      <select class="form-control" th:object="${editprovincia2th}" id="ddl" name="ddl"> 
      <option value="" th:text="#{editprofile.about4.provincia}">Seleccionar Provincia</option> 
      <option th:each="dropDownItem : ${editprovincia2th}" 
                th:value="${dropDownItem.id_provincia}" 
                th:text="${dropDownItem.provincia}"></option> 
      </select> 
     </div> 
    </section> 

が、私はどのように上の任意の作業例を見つけることができませんリスト内の値に対応する選択されたキーと値(th:select)を抽出し、それをcontに戻しますローラーを移動させる(または、有線オブジェクトを含むprofile2オブジェクトに移動する)。私はポストの後に私のコントローラでprovinciaのnull値を取得します。

HTMLに(select th:field = "* {provincia}")を含めると、エラーが表示されます...予期せぬエラー(type = Internal Server Error、status = 500) 。 プロセッサ 'org.thymeleaf.spring4.processor.attr.SpringSelectFieldAttrProcessor'(editprofileabout4:120)の実行中にエラーが発生しました。 Beanクラス[provinia]のプロパティ 'provincia'が無効です。 getterメソッドが無効です:getterの戻り値の型が、setterのパラメータの型と一致していますか?

ご協力ありがとうございます。 ご迷惑をおかけして申し訳ございません。

答えて

0

あなたはThymeleaf文書の状態としてので、select要素からth:object属性を削除し、フォームだけのための1を維持する必要があります:タグ内一度

、他の目:オブジェクト属性が を指定することはできません。これは、HTMLフォームを ネストすることはできないという事実と一致しています。

th:objectフォームの属性は、フォームバッキングBean、つまりフォームを介して送信する値を保持できるオブジェクトを参照する必要があります。あなたが財産に単に存在していないListオブジェクトのprovinciaにアクセスしようとしている瞬間にprovincia

+0

ありがとうFuudge。私がth:オブジェクトを削除すると、それも機能しません。 – Mike

+0