将Spring Form标签绑定到Map

时间:2014-11-16 21:14:17

标签: spring jsp

我要求弹出窗体标签绑定的命令对象包含一个Map,用于将关键字映射到“Y”或“N”。

在命令对象中,地图定义如下:

private Map<String, String> keywordResponseMap = new LinkedHashMap<String, String>();

在JSP中,我使用单选按钮生成Y / N响应:

    <c:forEach items="${form.eligibilityQuestions}" var="question">
        <c:set var="keyword" value="${question.keyword}" />
        <tr>
            <td width="75%">${question.text}</td>
            <td><form:radiobutton path="keywordResponseMap['${keyword}']" value="Y"/></td>
            <td><form:radiobutton path="keywordResponseMap['${keyword}']" value="N"/></td>
        </tr>
    </c:forEach>

单选按钮控件不会在表单提交时将Y或N绑定到地图中。

Chrome开发工具向我显示提交的请求参数如下所示(是的,空间是故意的,固定宽度数据的乐趣):

keywordResponseMap['RECORD4 ']:Y

有人可以帮忙吗?

杰森

1 个答案:

答案 0 :(得分:0)

我通过在后备控制器中创建后处理方法解决了这个问题:

public void postprocessQuestions(RequestContext requestContext) {

    log.debug("Entered postprocessQuestions()");

    TrainingForm form = (TrainingForm) requestContext.getFlowScope().get("trainingForm");

    // manually get the reservation responses from the external request
    // context parameters
    Map<String, Object> requestParamMap = requestContext.getExternalContext().getRequestParameterMap().asMap();
    for (String key: requestParamMap.keySet()) {
        if (key.startsWith("keywordResponseMap")) {
            String keyword = StringUtils.substringBefore("['", "']");
            String value = (String) requestParamMap.get(keyword);
            form.getKeywordResponseMap().put(keyword, value);
        }
    }

}

然后我必须关闭转换的自动验证,以便在验证之前运行postprocessign方法:

    <view-state id="renoExtendedEligibility" view="renoExtendedEligibility" model="renegotiationForm">
        <on-render>
            <evaluate expression="flowController.enterQuestions(flowRequestContext)" /> 
        </on-render>
        <transition on="prev" to="QuestionsTransition" validate="false" />
        <transition on="*" to="QuestionsTransition" validate="false">
            <evaluate expression="renoFlowController.postprocessQuestions(flowRequestContext)" />
            <evaluate expression="renegotiationFormValidator.validateQuestions(renegotiationForm, validationContext)" />
        </transition>
    </view-state>

感谢您的帮助!

杰森