0
我試圖綁定一個對象與一個地圖字段在GET上初始化呈現多個複選框標記。所有複選框顯示何時創建表單,但是何時提交,我的MessageForm(模型屬性)對象中的Map條目都沒有綁定(映射大小= 0)。在添加此地圖字段之前,其他字段(消息)正在設置好。如何使用MessageForm.hierarchySelections字段來設置GET請求中填充的所有條目?春天MVC 3 - 表單對象與地圖屬性不綁定鍵/值對錶單提交
unitNode.jsp(VIEW_MESSAGE_FORM):
<div class="nodeContainer">
<div class="nodeHeader">
<form:checkbox path="hierarchySelections['${node.code}']"/>
<form:label path="hierarchySelections['${node.code}']">
${node.name}
</form:label>
</div>
<div class="nodeChildren">
<c:forEach var="node" items="${node.children}">
<c:set var="node" value="${node}" scope="request"/>
<jsp:include page="unitNode.jsp"/>
</c:forEach>
</div>
</div>
MessageForm.java:
public class MessageForm {
private Message message;
private Map<String, Boolean> hierarchySelections = new HashMap<String, Boolean>();
// getters and setters
}
MessageFormController.java(節選):
@RequestMapping(value = "/message/new")
public String newMessage(final Model model) {
final MessageForm messageForm = new MessageForm();
// get the root hierarchy node
final Node rootNode = hierarchyService.getNodeHierarchy();
messageForm.getHeirarchy(rootNode);
final Stack<Node> nodeList = new Stack<Node>();
nodeList.add(rootNode);
final Map<String, Boolean> hierarchySelections = messageForm.getHierarchySelections();
while (!nodeList.isEmpty()) {
final Node node = nodeList.pop();
// set the selection status to false/unchecked
hierarchySelections.put(node.getCode(), Boolean.FALSE);
// add all children organization units to the stack
for (final Node nodeChild : node.getChildren()) {
nodeList.add(nodeChild);
}
}
model.addAttribute("messageForm", messageForm);
return VIEW_MESSAGE_FORM;
}
@RequestMapping(value = "/message/new", method = RequestMethod.POST)
public String createMessage(@Valid final MessageForm messageForm, final BindingResult bindingResult) {
if (bindingResult.hasErrors()) { // TODO
} else {
messageCenterService.createMessage(messageForm.getMessage());
}
return VIEW_MESSAGE_FORM;
}