2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Basic difference between spring:bind and form:errors in spring

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

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


userimage

With spring:bind, you can use ${status.error} to check if the name1 field has an error, and display different CSS class conditionally.

The error message is still displayed via form:errors, but this way you get more controls.

for example:


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

Answer is