Spring MVC:spring:bind和form:form的区别

14

我想了解在提交表单时,spring:bind和form:form标签库之间的区别。

我的JSP代码片段如下:

<form:form modelAttribute="testRulesForm">
....
<td>
    <form:checkbox path="rules[${counter.index}].isActive" value="rules[${counter.index}].isActive"/>
</td>
<td>
    <form:select path="rules[${counter.index}].leftCondition.name">
        <form:options items="${testRulesForm.ruleAttributes}" itemLabel="name" itemValue="name" />
    </form:select>
</td>
<td>
    <form:select path="rules[${counter.index}].operator">
        <form:options itemLabel="operator" itemValue="operator" />
    </form:select>
</td>
....

既然我已经指定了我的路径变量并将其绑定到我的modelAttribute中,那么这是否意味着我不需要使用spring:bind?

谢谢。

1个回答

27

一般情况下,如果您已经使用了form标签库,就不需要使用<spring:bind>

它们在模型属性方面基本相同,但来自form标签库的标记还会生成HTML表单标记,而对于<spring:bind>,您需要自己生成标记。

以下是使用form标签的代码:

<form:form modelAttribute = "foo">
    <form:input path = "bar" />
</form:form>

与使用 <spring:bind> 的代码类似:

<spring:bind path = "foo">
    <form method = "get">
        <spring:bind path = "bar">
            <input name = "bar" value = "${status.displayValue}" />
        </spring:bind>
    </form>
</spring:bind>

<spring:bind> 在需要自定义一些无法通过 form 标签库实现的情况下非常有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接