spring:bind

org.springframework.web.servlet.tags.BindTag


spring:bind (since 1.0)
General information
The spring:bind tag provides you with support for evaluation of the status of a certain bean or bean property. The status of a bean includes the value of the actual bean or bean property you're evaluating as well as possibily available errors and the expression to use in forms in order for the databinding functionality to be able to bind the properties again when submitting for instance a form.
Attributes
path
The path to the bean or bean property to bind status information for. For instance account.name, company.address.zipCode or just employee. The status object will exported to the page scope, specifically for this bean or bean property
required: yes
el-support: yes
ignoreNestedPath
Set whether to ignore a nested path, if any. Default is to not ignore.
required: no
el-support: no
htmlEscape
Set HTML escaping for this tag, as boolean value. Overrides the default HTML escaping setting for the current page.
required: no
el-support: yes
Variables
status
The status object, giving you information about errors, property values and expressions to use when rendering e.g. form fields
type: org.springframework.web.servlet.support.BindStatus
status.expression: the expression that was used to retrieve the bean or property
status.value: the actual value of the bean or property (transformed using registered PropertyEditors)
status.errorMessages: an array of error messages, resulting from validation
Also have a look at
the spring:transform tag, to see how to also transform reference data values using property editors
the spring:message tag, to see how you can internationalize your error messages
the ServletRequestDataBinder and the registerCustomEditor method in DataBinder to see how the property editing works
the BaseCommandController for more information about command objects (your data objects) and how they works
the spring:nestedPath tag, which allows you to set a nested bean or bean property path
A possible usecase
Consider the following:
  • SimpleFormController that controls Company objects
  • Company has a name and an address property, where address is of type Address
  • Address has three properties, i.e. street, zipCode and city
  • in your formBackObject you have already instantiated the empty Company object containing an empty Address object
To display a form rendering all properties (that - in case of validation failures - forward to the formView and fills in all properties that were already set) and also displaying errors:
<form method="post">
    ## first bind on the object itself to display global errors - if available
    <spring:bind path="company">
        <c:forEach items="${status.errorMessages}" var="error">
        Error code: <c:out value="${error}"/><br>
        </c:forEach>
    </spring:bind>
    
    ## if you need to display all errors (both global and all field errors,
    ## use wildcard (*) in place of the property name
    <spring:bind path="company.*">
        <c:forEach items="${status.errorMessages}" var="error">
        Error code: <c:out value="${error}"/><br>
        </c:forEach>
    </spring:bind>
	
    ## 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}"/>">
            ## if there are error codes, display them!
            <c:if test="${status.error}">
                Error codes:
                <c:forEach items="${status.errorMessages}" var="error">
                    <c:out value="${error}"/>
                </c:forEach>
            </c:if>
    </spring:bind>
	
    <spring:bind path="company.address.street">
        Name: <input 
            type="text"
            value="<c:out value="${status.value}"/>"
            name="<c:out value="${status.expression}"/>">
            <c:if test="${status.error}">
                Error codes:
                <c:forEach items="${status.errorMessages}" var="error">
                    <c:out value="${error}"/>
                </c:forEach>
            </c:if>
    </spring:bind>
	
    ## same thing for zipCode
	
    <input type="submit">
</form>