0

我正在尝试将对象与在 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;
}
4

1 回答 1

1

我认为复选框序列化不是您期望的那样工作。

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

所以首先,使用 Firebug/Chrome 调试器(网络选项卡)来监控从浏览器发送到服务器的信息。

于 2013-06-20T14:15:20.417 回答