Spring 表单 + JSP:将 <select> 的值绑定到地图 <String, String> - 我做错了什么
Spring forms + JSP: Binding the value of a <select> to a map<String, String> - what am I doing wrong
我正在使用 spring 3.0.5 和 tomcat 7
我在做什么:
我在 JSP 中创建了 table 个 select 个元素。每个 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;
}
问题:
如果我 select 下拉列表中的值并提交此表单,我可以在控制器中放置一个断点,然后看到 'tempNames' 中包含所有正确的值,我可以处理并保存——这正是我所期望的——所以绑定以一种方式工作……
但是,如果 'someStuff' 中已有值,则这些值不会绑定到下拉列表。
我试过了
向元素本身添加 'value' 属性,例如:
<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">
但是第二个似乎甚至没有出现在结果 HTML 中......
我遇到了同样的问题。
看来你必须自己计算 option
的 selected
标志:
<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
属性的内容,唯一重要的是属性本身的 presence/absense:selected="false"
仍然使选项被选中。
我正在使用 spring 3.0.5 和 tomcat 7
我在做什么:
我在 JSP 中创建了 table 个 select 个元素。每个 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;
}
问题:
如果我 select 下拉列表中的值并提交此表单,我可以在控制器中放置一个断点,然后看到 'tempNames' 中包含所有正确的值,我可以处理并保存——这正是我所期望的——所以绑定以一种方式工作……
但是,如果 'someStuff' 中已有值,则这些值不会绑定到下拉列表。
我试过了
向元素本身添加 'value' 属性,例如:
<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">
但是第二个似乎甚至没有出现在结果 HTML 中......
我遇到了同样的问题。
看来你必须自己计算 option
的 selected
标志:
<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
属性的内容,唯一重要的是属性本身的 presence/absense:selected="false"
仍然使选项被选中。