java <spring:bind> 有什么用?什么时候用,什么时候不用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6851332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is <spring:bind> for? When to use it, and when not to use it?
提问by Wellingr
I am having problems with submitting form data in spring. <spring:bind>
seems to be a part of the solution. See my full problem here.
我在春季提交表单数据时遇到问题。<spring:bind>
似乎是解决方案的一部分。在这里查看我的完整问题 。
The documentation of BindTag found hereis not clear to me. Why is <spring:bind>
needed in some cases to submit data, while it is not needed in most cases?
我不清楚此处找到的 BindTag 文档。为什么<spring:bind>
在某些情况下需要提交数据,而在大多数情况下不需要?
What are the typical cases where the <spring:bind>
must be used in order for a form to function properly?
<spring:bind>
为了使表单正常运行,必须使用的典型情况是什么?
回答by Rocío García Luque
You will find the tag <spring:bind>
useful when you want to parse multiple objects from an input form. Here's a modified example from the Spring's doc (http://docs.spring.io/spring/docs/1.2.6/taglib/tag/BindTag.html):
<spring:bind>
当您想从输入表单解析多个对象时,您会发现该标签很有用。这是 Spring 文档中的修改示例(http://docs.spring.io/spring/docs/1.2.6/taglib/tag/BindTag.html):
<form method="post">
## now bind on the name of the company
<spring:bind path="company.name">
## render a form field, containing the value and the expression
Name: <input
type="text"
value="<c:out value="${status.value}"/>"
name="<c:out value="${status.expression}"/>">
</spring:bind>
<spring:bind path="address.street">
Name: <input
type="text"
value="<c:out value="${status.value}"/>"
name="<c:out value="${status.expression}"/>">
</spring:bind>
<input type="submit">
</form>
回答by DCKing
Although I've never used this tag myself, my understanding of the documentation is this. A tag will provide you with information about the binding status of a form property to a bean. For example in:
虽然我自己从未使用过这个标签,但我对文档的理解是这样的。标签将为您提供有关表单属性与 bean 的绑定状态的信息。例如在:
<form:form modelAttribute="employee">
<form:input path="name"/>
<spring:bind path="name"/>
<spring:bind path="employee"/>
</form:form>
The tag will display (or expose through a BindStatus object) all errors that have occurred with the name attribute (the first case) and all errors on the Employee entity and its attributes (the second case). I am not sure that this tag has anything to do with the succesfulness of submitting data, but rather that it's used as an information tool.
该标记将显示(或通过 BindStatus 对象公开)所有发生在 name 属性上的错误(第一种情况)以及 Employee 实体及其属性上的所有错误(第二种情况)。我不确定这个标签与提交数据的成功有什么关系,而是它被用作信息工具。