21

我正在将 Spring MVC 用于 Web 应用程序。在表格上,我有

<form:checkbox path="agreeTerms" id="agreeTerms"/>

渲染页面时,会生成以下 HTML

<input id="agreeTerms" type="checkbox" value="true" name="agreeTerms">
<input type="hidden" value="on" name="_agreeTerms">

有人知道隐藏标签的用途吗?如果隐藏的输入标签被删除会发生什么?

谢谢!

4

1 回答 1

24

隐藏的输入标记用于指示最初是表单一部分的字段。提交表单时,复选框输入字段仅在具有值(即“已选中”)时才会发送。如果未选中,则不发送任何内容。下划线前缀隐藏字段用于指示它是表单的一部分,但应默认为“未选中/假”。

您可以通过创建一个带有复选框字段的 HTML 表单并在不选中该字段的情况下提交它来测试这一点。

另外,要查看它是如何完成的,请查看WebDataBinder的源代码:

/**
* Check the given property values for field markers,
* i.e. for fields that start with the field marker prefix.
* <p>The existence of a field marker indicates that the specified
* field existed in the form. If the property values do not contain
* a corresponding field value, the field will be considered as empty
* and will be reset appropriately.
* @param mpvs the property values to be bound (can be modified)
* @see #getFieldMarkerPrefix
* @see #getEmptyValue(String, Class)
*/
于 2013-06-28T15:45:02.337 回答