我正在尝试将对象与在GET上初始化的Map字段绑定,以呈现多个复选框标签。所有复选框都会在创建表单时显示,但是在提交表单时,不会绑定MessageForm(模型属性)对象中的所有Map条目(地图大小= 0)。在添加此地图字段之前,其他字段(消息)的设置都很好。如何获取MessageForm.hierarchySelections字段,以对GET请求中填充的所有条目进行设置?

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;
}

最佳答案

我认为复选框序列化的工作方式与您期望的不同。

未选中的复选框元素不会提交,当选中时,来自value属性的文本将被发送。

因此,首先,使用Firebug / Chrome调试器(“网络”标签)来监视从浏览器发送到服务器的信息。

08-04 03:58