2016-07-13 19 views
1

を何をやっている私は私がやっている何を春3.0.5とTomcat 7春フォーム+ JSP:マップ<文字列、文字列>に<select>の値をバインド - 私が間違っ

を使用しています:

私はJSPでselect要素のテーブルを作成しました。それぞれのは、このように、私のモデルにIDに関連付けられている選択:

<form:form commandName="someStuff" method="post" id="peopleForm" onSubmit="return validate('${someStuff}');"> 

<%-- A bunch of other unrelated stuff here --%> 
<table> 
    <th>some other info</th> 
    <th>please select a name</th> 
    <c:forEach var="thisThing" items="${allOfTheThings}"> 
     <tr> 
     <td>some other info here</td> 
     <td> 
      <form:select id="name_for_thing_${thisThing.SomeId}" path = "tempNames['${thisThing.SomeId}']" thisThingsId="${thisThing.SomeId}" class="thing_name_selector_class"> 
       <option value="Please Select One">Please Select One</option> 
       <option value="John">John</option> 
       <option value="Joe">Joe</option> 
       <option value="Stephen">Stephen</option> 
       <option value="Mary">Mary</option> 
      </form:select > 
     </td> 
     </tr> 
    </c:forEach> 
</form:form> 

、その後、私のドメインオブジェクトのsomeStuff "

に私のようなマップを定義します。

private Map<String, String> tempNames; 

public Map<String, String> getTempNames() { 
    return tempNames; 
} 

public void setTempNames(Map<String, String> tempNames) { 
    this.tempNames = tempNames; 
} 

問題を:

ドロップダウンから値を選択してこのフォームを送信すると、コントローラにブレークポイントを設定して、 'tempNames'に正しい値がすべて入っていることを確認できますこれはちょうど私が期待しているものなので、バインディングは一方向に働きます...

しかし、someStuffに既に値が入っていれば、それらの値はドロップダウンに束縛されません。

私は次のように要素自体に 'の値' 属性を追加する

を試してみた:

このような
<form:select id="name_for_thing_${thisThing.SomeId}" path = "tempNames['${thisThing.SomeId}']" value="tempNames['${thisThing.SomeId}']" thisThingsId="${thisThing.SomeId}" class="thing_name_selector_class"> 

とも:

<form:select id="name_for_thing_${thisThing.SomeId}" path = "tempNames['${thisThing.SomeId}']" value="${tempNames[thisThing.SomeId]}" thisThingsId="${thisThing.SomeId}" class="thing_name_selector_class"> 

しかし、第2 1にはありません結果のHTMLにも表示されているようです....

答えて

1

私は同じ問題を抱えてきました。 は自分でoptionselectedフラグを計算しなければならないようだ:

<form:select id="name_for_thing_${thisThing.SomeId}" path = "tempNames['${thisThing.SomeId}']" thisThingsId="${thisThing.SomeId}" class="thing_name_selector_class"> 
     <option value="Please Select One">Please Select One</option> 
    <c:choose> 
     <c:when test="${tempNames['${thisThing.SomeId}'] == "John"}"> 
      <option value="John" selected="true">John</option> 
     </c:when> 
     <c:otherwise> 
      <option value="John">John</option> 
     </c:otherwise> 
    </c:choose> 

    ... 
</form:select > 

<option selected="${tempNames['${thisThing.SomeId}'] == "John"}">が動作しませんのでご注意ください。ブラウザ(少なくともChrome)はselected属性の内容を無視しますが、重要なのは属性自体の存在/不在のみです。selected="false"は依然としてそのオプションを選択します。

関連する問題