2016-08-28 1 views
1

enter code here私はコントローラと、メンバーという名前のビジネスオブジェクトのGETとPOSTのためにマップされた2つ以下のURLを持っています。JSTL <form:select>タグをjspで使用すると正しく表示されない表示

コントローラー:次のように

@RequestMapping(value = "/usr/updateMember", method = RequestMethod.GET) 
public ModelAndView findForEdit(@RequestParam("id") Integer id) { 
    ModelAndView modelAndView = new ModelAndView("registration"); 
    try { 
     List<City> cities = getGenericDao().getAllItems(City.class); 
     List<Chapter> chapters = getGenericDao().getAllItems(Chapter.class); 
     modelAndView.getModel().put("cities", cities); 
     modelAndView.getModel().put("chapters", chapters); 

     Member member = getGenericDao().getItem(Member.class, id); 
     modelAndView.getModel().put("edit", true); 
     modelAndView.getModel().put("loggedinuser", getPrincipal()); 
     modelAndView.getModel().put("roles", userRoleService.findAll()); 

     modelAndView.getModel().put("member", member); 
    } catch (Exception e) { 
     System.out.println(e.getMessage()); 
    } 
    return modelAndView; 
} 

@RequestMapping(value = "/usr/updateMember", method = RequestMethod.POST) 
public String update(@ModelAttribute("member") Member member, BindingResult result, SessionStatus status) { 
    getGenericDao().update(member); 
    status.setComplete(); 
    return "redirect:/list.do"; 
} 

私の見解は以下のとおりです。

<form:form method="POST" modelAttribute="member" 
      class="horizForm"> 
    <div class="row"> 
     <div class="form-group col-md-"> 
      <label class="col-md-3" for="name">Member 
       Name</label> 
      <div class="col-md"> 
       <form:input type="text" path="name" id="name" 
        class="form-control input-sm" /> 
       <div class="has-error"> 
        <form:errors path="name" class="help" /> 
       </div> 
      </div> 
     </div> 
    </div> 
    ... 
    <div class="row"> 
     <div class="form-group col-md-12"> 
      <label class="col-md-3" for="userRoles">Roles</label> 
      <div class="col-md"> 
       <form:select path="userRoles" items="${roles}" multiple="true" 
        itemValue="memberId" itemLabel="role" 
        class="form-control input-sm" /> 
       <div class="has-error"> 
        <form:errors path="userRoles" class="help" /> 
       </div> 
      </div> 
     </div> 
    </div> 

    <div class="row"> 
    <div class="form-group col-md-"> 
     <label class="col-md-3 control-labl" for="chapter">Chapter</label> 
     <div class="col-md-7"> 
      <%-- <form:select path="chapter.id" items="${chapters}" itemValue="id" 
       itemLabel="name" class="form-control input-sm"> --%> 
      <%-- <option value="-1">Select a Chapter</option> 
       <c:forEach var="chp" items="${chapters}"> 
        <option value="${chp.id}">${chp.name}</option> 
       </c:forEach> --%> 
      <%-- </form:select> --%> 

      <form:select path="chapter" class="form-control input-sm"> 
       <!-- <option value="-1">Select a Chapter</option> --> 
       <c:forEach var="chp" items="${chapters}"> 
        <option value="${chp}">${chp.name}</option> 
       </c:forEach> 
      </form:select> 

      <div class="has-error"> 
       <%-- <form:errors path="chapter.id" class="help-inline" /> --%> 
       <form:errors path="chapter" class="help-inline" /> 
      </div> 
     </div> 
    </div> 
</div> 

GETは期待通りに働いているとビューは文字列、リストや章タイプ・オブジェクトの期待値が取り込まれます。

GET Method working fine

私はそれがリストにバインド値と章オブジェクトを取得していないとBindingResultは値の下に持ってPOSTを経由してメンバーを更新しようとします。

org.springframework.validation.BeanPropertyBindingResult: 3 errors 
Field error in object 'member' on field 'chapter': rejected value [Chapter [chapterId=1, chapterName=SUPER]]; codes [typeMismatch.member.chapter,typeMismatch.chapter,typeMismatch.com.codex.survey.beans.Chapter,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [member.chapter,chapter]; arguments []; default message [chapter]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.codex.survey.beans.Chapter' for property 'chapter'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.codex.survey.beans.Chapter] for property 'chapter': no matching editors or conversion strategy found] 
Field error in object 'member' on field 'userRoles': rejected value [2]; codes [typeMismatch.member.userRoles,typeMismatch.userRoles,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [member.userRoles,userRoles]; arguments []; default message [userRoles]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'userRoles'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.codex.survey.beans.UserRole] for property 'userRoles[0]': no matching editors or conversion strategy found] 

ここでは何が問題になっているのかわかりません。この問題を解決するのに役立ちます。

ありがとうございます。

答えて

0

ステートフルなコンポーネントベースのフレームワーク(JSFなど)とは異なり、Spring MVCは生のHTTPパラメータを対応するオブジェクト型に自動的に変換することはできません。

したがって、これを処理してSpringのコンテキストに登録するコンバータを登録する必要があります。 1つの実装は次のようになります:

@Component 
public class StringToChapterConverter implements Converter<String, Chapter> { 

    @Autowired 
    private ChaperService service; 

    /** 
    *chapterId is the HTTP post param passed in by the framework 
    */ 
    public Chapter convert(String chapterId) { 
     return service.findChapter(Integer.parseInt(chapterId)); 
    } 
} 

これを登録する方法は、設定によって異なります。しかし、その後のJavaコードの設定を想定して:

だから、
@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = { "..." }) 
public class MvcConfiguration extends WebMvcConfigurerAdapter{ 

    //other configuration 

    @Override 
    public void addFormatters(FormatterRegistry formatterRegistry) { 
     formatterRegistry.addConverter(getChapterConverter()); 
    } 

    @Bean 
    public StringToChapterConverter getChapterConverter(){ 
     return new StringToChapterConverter(); 
    } 
} 

、今以上のある場所での要求が行われたときにフレームワークは、適切なコンバータが章に定義されているかどうかを確認するために見ていきます章オブジェクトにバインドされていますデータベースから、またはどこからでもインスタンスを返すことができる対応するconvert(String source)を呼び出します。

をさらに参照:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html#core-convert

+0

おかげアラン・ヒントのために、私は、コンバータの一部が欠落して、チェックし、私はそれが動作することを願っています。 –

+0

アランの提案が私の問題を解決しました。 –

関連する問題