Domanda

What is <spring:hasBindErrors>?What is its use?

I tried to Google it but could not find any useful content.

È stato utile?

Soluzione

The spring:hasBindErrors is a spring tag that provides you with errors bound for an object(generally form). Errors are set in the validation method of the form object. If the binding form object has errors, then errors will be available in pageScope.

You can set the errors as below:

Form object:

    public class YourForm implements Serializable{
        private String name;
        private String company;
        //mutators
        ... 
    }

You are validating this form in a validator with validation method as:

    public class YourValidator implements Validator{

        public boolean supports(Class<?> clazz) {
            return clazz.equals(YourForm.class);
        }

        public void validateYourViewName(YourForm yourForm, Errors errors) {
            YourForm yourForm = (YourForm)object;
            if (yourForm.getName() == null || yourForm.getName().length() == 0){
                errors.rejectValue("name", "name.required", "Name field is missing");
            }
        }
        ...
    }

In your jsp, you can see errors as:

    <spring:hasBindErrors name="yourForm">
        <c:forEach var="error" items="${errors.allErrors}">
        <b><spring:message message="${error}" /></b>
        <br/>
        </c:forEach>
    </spring:hasBindErrors>

Attributes in the tag: name: form name that is bound or validated.
You can also get more details from errors: errors.errorCount: the number of errors errors.allErrors: all errors errors.globalErrors: the errors registerd for the object

You can find more details as to what you can retrieve and view from errors object here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top