【问题标题】:Spring forms + JSP: Binding the value of a <select> to a map<String, String> - what am I doing wrongSpring forms + JSP:将 <select> 的值绑定到 map<String, String> - 我做错了什么
【发布时间】:2016-07-13 18:36:16
【问题描述】:

我正在使用 spring 3.0.5 和 tomcat 7

我在做什么:

我在 JSP 中创建了一个选择元素表。每个选择都与我的模型中的一个 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”中包含所有正确的值,我可以处理和保存 - 这正是我所做的'd期望 - 所以绑定以一种方式工作......

但是,如果“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">

但是第二个似乎甚至没有出现在生成的 HTML 中......

【问题讨论】:

    标签: spring jsp spring-form


    【解决方案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 >
    

    请注意&lt;option selected="${tempNames['${thisThing.SomeId}'] == "John"}"&gt; 不起作用。浏览器(至少 Chrome)会忽略 selected 属性的内容,唯一重要的是属性本身的存在/不存在:selected="false" 仍然使选项被选中。

    【讨论】:

      猜你喜欢
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多