【问题标题】:Spring MVC: Difference between spring:bind and form:formSpring MVC:spring:bind 和 form:form 的区别
【发布时间】:2011-12-21 14:47:57
【问题描述】:

我希望在提交表单时找出 spring:bind 和 form:form 标签库之间的区别。

我的JSP的一个sn-p如下:

<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?

谢谢

【问题讨论】:

    标签: forms jsp spring-mvc


    【解决方案1】:

    如果您已经使用form taglib,通常不需要使用&lt;spring:bind&gt;

    它们在模型属性方面基本相同,但来自form taglib 的标签也会生成 HTML 表单标记,而使用 &lt;spring:bind&gt; 你需要自己生成标记。

    以下代码带有form标签:

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

    类似于下面带有&lt;spring:bind&gt;的代码:

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

    &lt;spring:bind&gt; 在您需要定制的东西时很有用,而form taglib 无法实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 2021-08-01
      相关资源
      最近更新 更多