什么是 <spring:hasBindErrors>?

a L*_*ner 4 spring spring-mvc

什么是<spring:hasBindErrors>?有什么用?

我试着用谷歌搜索但找不到任何有用的内容。

Pra*_*sad 5

弹簧:hasBindErrors是弹簧标记,提供了用于结合的物体(通常形式)错误。错误是在表单对象的验证方法中设置的。如果绑定表单对象有错误,则错误将在 pageScope 中可用。

您可以设置错误如下:

表单对象:

    public class YourForm implements Serializable{
        private String name;
        private String company;
        //mutators
        ... 
    }
Run Code Online (Sandbox Code Playgroud)

您正在使用验证方法在验证器中验证此表单:

    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");
            }
        }
        ...
    }
Run Code Online (Sandbox Code Playgroud)

在您的 jsp 中,您可以看到以下错误:

    <spring:hasBindErrors name="yourForm">
        <c:forEach var="error" items="${errors.allErrors}">
        <b><spring:message message="${error}" /></b>
        <br/>
        </c:forEach>
    </spring:hasBindErrors>
Run Code Online (Sandbox Code Playgroud)

标签中的属性: name:绑定或验证的表单名称。
您还可以从错误中获取更多详细信息:errors.errorCount:错误数量 errors.allErrors:所有错误 errors.globalErrors:为对象注册的错误

您可以在此处找到有关可以从错误对象中检索和查看的内容的更多详细信息。