What is the difference between spring: bind and form: errors?

I have the following form in Spring that shows error messages. I am wondering when to use spring: bind? How is this different from this? I studied these pages a , b but I'm still puzzled.

1

<form:form method="post" action="http://localhost:8080/project/calculator/process" modelAttribute="keyPadForm"> Name1: <form:input type="text" path="name1" /> <form:errors path="name1" /> 

2

 <form:form method="post" action="http://localhost:8080/project/calculator/process" modelAttribute="keyPadForm"> <spring:bind path="name1"> Name1: <form:input type="text" path="name1" /> <form:errors path="name1" /> </spring:bind> 
+6
source share
2 answers

In your second case, the spring:bind tag is out of date, your first form

 <form:form method="post" action="http://localhost:8080/project/calculator/process" modelAttribute="keyPadForm"> Name1: <form:input type="text" path="name1" /> <form:errors path="name1" /> 

is a kind of syntactic sugar, and the equivalent without using the form tag library, and not just common HTML form tags, will be based on spring:bind and will look something like this:

 <spring:nestedPath path="keyPadForm"> <form method="post" action="http://localhost:8080/project/calculator/process"> <spring:bind path="name1"> Name1:<input type="text" name="${status.expression}" value="${status.value}"> <span class="fieldError">${status.errorMessage}</span> </spring:bind> </form> </spring:nestedPath> 

There are scenarios in which you can make a difference, for example. form:input always a two-way binding, so the value is sent to the server and the current value displayed, where, as in the case of spring:bind , you can achieve one-way binding, only send to the server, omitting the value, for example. <input type="text" name="${status.expression}"> but the main theme is that the form tag library provides more convenient tags related to binding

+2
source

With spring:bind you can use ${status.error} to check if there is an error in the name1 field and conditionally display another CSS class.
The error message is still displayed via form:errors , but this way you get more controls.
eg:

 <form:form method="post" modelAttribute="userForm" action="${userActionUrl}"> <spring:bind path="name"> <div class="form-group ${status.error ? 'has-error' : ''}"> <label>Name</label> <form:input path="name" type="text" id="name" /> <form:errors path="name" /> </div> </spring:bind> </form:form> 

and you can refer to this Spring MVC Form - check if there is an error message in the field

+2
source

All Articles