spring - FieldError vs ObjectError vs Global Error -
i'm reading through spring 4 api , i'm trying understand difference between fielderror, objecterror , global error, in context of bindingresult. i'm guessing global errors name objecterror
s since getglobalerror() returns objecterror
.
the context looking print error messages after form validation , want know how can avoid instanceof check, in this accepted answer. can use fielderror
, ignore object errors? missing if logged fielderror
s?
i've tried few scenarios don't yet see distinction. through source in meantime.
i'm guessing global errors name objecterrors since getglobalerror() returns objecterror.
actually "global error" objecterror
not instance of fielderror
see source of getglobalerrors()
what missing if logged fielderrors?
any objecterror
s code registered "global error" e.g. calling bindingresult.reject(errorcode, errorargs, defaultmessage)
.
see javadoc rejectvalue(field, errorcode, errorargs, defaultmessage)
. typically errors registered against fields of validated/bound object (e.g. model value attribute matches modelattribute
tag of spring form tag) opposed object itself.
following couple of ways create global errors:
assuming it's root form object , not nested object that's being validated via spring
validator
implementation, add "global error" (in context of specified bound root object) passing null field name parameter ofrejectvalue
. if object being validated nested object, however,fielderror
registered against nested object field. mattersnestedpath
("nested object graph") property of targeterrors
object respect whether generalobjecterror
or specificfielderror
added.via jsr 303 constraint annotation applied @ class level. see example in model object checked pre-existence in datastore.
here's example reference global vs field level errors:
public class overflowerrorstag extends htmlescapingawaretag { public static final string overflow_errors_variable_name = "overflowerrors"; public static final string global_errors_variable_name = "globalerrors"; private string name; /** * set name of bean tag should check. */ public void setname(string name) { this.name = name; } /** * return name of bean tag checks. */ public string getname() { return this.name; } @override protected final int dostarttaginternal() throws servletexception, jspexception { errors errors = getrequestcontext().geterrors(this.name, ishtmlescape()); set<fielderror> subsequenterrors = sets.newtreeset((fe1, fe2) -> fe1.getfield().compareto(fe2.getfield())); set<objecterror> globalerrors = new hashset<>(); if (errors != null) { set<string> firsterrorfields = new hashset<>(); (fielderror fielderror : errors.getfielderrors()) { if (firsterrorfields.contains(fielderror.getfield())) { subsequenterrors.add(fielderror); } else { firsterrorfields.add(fielderror.getfield()); } } (objecterror objecterror : errors.getglobalerrors()) { globalerrors.add(objecterror); } } if (subsequenterrors.isempty() && globalerrors.isempty()) { return skip_body; } else { this.pagecontext.setattribute(overflow_errors_variable_name, subsequenterrors, pagecontext.request_scope); this.pagecontext.setattribute(global_errors_variable_name, globalerrors, pagecontext.request_scope); return eval_body_include; } } @override public int doendtag() { this.pagecontext.removeattribute(overflow_errors_variable_name, pagecontext.request_scope); this.pagecontext.removeattribute(global_errors_variable_name, pagecontext.request_scope); return eval_page; } }
then display tag containing both global , field errors in view:
<spring-ext:overflowerrors name="newmodelobject"> <div class="row"> <div class="large-12 columns"> <div class="alert panel"> <c:if test="${overflowerrors.size()>0}"> <p>there multiple errors entry.</p> <c:foreach var="error" items="${overflowerrors}"> ${fn:touppercase(fn:substring(error.field, 0, 1))}${fn:tolowercase( fn:substring(error.field, 1,fn:length(error.field)))}: <b><spring:message message="${error}" /></b> <br/> </c:foreach> </c:if> <c:foreach var="error" items="${globalerrors}"> <b><spring:message message="${error}" /></b> <br/> </c:foreach> </div> </div> </div> </spring-ext:overflowerrors>
Comments
Post a Comment