这个问题与这个问题非常相似: Collection/List property won't bind or update on form submit
我想要做的是将 JSP 中的一组复选框映射到 webflow 中的地图。将例如字符串绑定到模型完全可以正常工作。但是,地图没有。下面是一些示例代码: 模型:
public class MyForm {
private String selectedOrderBy;
private Map<String, boolean> selected = new HashMap<>();
private List<MyClass> items = new ArrayList<>();
//Now setters and getters for the members
}
public MyClass {
private String hash = "<some hash>"; //plus getter and setter
}
流量:
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<var name="model" class="MyForm"/>
<var name="selectedOrderBy" class="java.lang.String"/>
<view-state id="selection" model="model">
<binder>
<binding property="selectedOrderBy"/>
<binding property="selected"/>
</binder>
<transition on="submit" to="saveSelection"/>
</view-state>
<action-state id="saveSelection">
<evaluate expression="MyService.saveSelection(model, externalContext.nativeRequest)"/>
<transition to="selection"/>
</action-state>
</flow>
JSP:
<form:form modelAttribute="model" method="post" enctype="application/x-www-form-urlencoded" acceptCharset="utf-8">
...here is some more code including a selectbox mapping to selectOrderBy...
<table>
<c:foreach items="${model.items}" var="item">
<tr><td>
<form:checkbox path="selected['${item.hash}']" value="true"/>
...
</td></tr>
</c:foreach>
</table>
</form:form>
最后是服务:
@Named
public class MyService {
public void saveSelection(MyForm model, HttpServletRequest request){...}
}
复选框成功显示在地图中找到的值。但是,在提交时,更改的值不会绑定到模型(而字符串“selectOrderBy”有效)。所以我调试到 MyService#saveSelection,我发现模型中的地图仍然有旧值。同时,新值实际上在请求中。
所以目前,我手动从请求中提取它们:
String selected = request.getParameter("selected['" + hash + "']");
但这是一些非常丑陋的解决方法。有人知道为什么会这样吗?
问候,萨沙。