Wellingr
Wellingr

Reputation: 1191

What is <spring:bind> for? When to use it, and when not to use it?

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.

The documentation of BindTag found here is not clear to me. Why is <spring:bind> needed in some cases to submit data, while it is not needed in most cases?

What are the typical cases where the <spring:bind> must be used in order for a form to function properly?

Upvotes: 16

Views: 22429

Answers (2)

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):

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

Upvotes: 7

DCKing
DCKing

Reputation: 4273

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:

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

Upvotes: 1

Related Questions