Cannot display errors with spring <form:errors/> -
i not able spring validation errors displayed on jsp page. here code. on jsp page, when enter empty name, controller code return modelandview errors, doesn't display on jsp page.
any appreciated. thank you!
@requestmapping(value = "/edittag.htm", method = requestmethod.post) public modelandview edittag(@modelattribute("tag") tag tag) { bindingresult result = new beanpropertybindingresult(tag, "tag"); validationutils.rejectifemptyorwhitespace(result, "name", "field.required", "tag name required"); if (result.haserrors()) { return new modelandview("tag.edit").addobject("tag",tag).addobject("errors", result); } tagdao.merge(tag); return new modelandview("redirect:/tags/listtags.htm"); } <form:form commandname="tag"> <form:errors path="name"/><br /> <form:input path="name" size="30" /> ... </form:form>
you constructing new bindingresult whereas there 1 provided (and used in background) spring. adding bindingresult method right after @modelattribute annotated parameter gives this. can model result , use construct modelandview.
also observe modelattribute name (currently tag) doesn't match 1 used in form (tag). 2 should match.
something following should work.
@requestmapping(value = "/edittag.htm", method = requestmethod.post) public modelandview edittag(@modelattribute("tag") tag tag, bindingresult result) { validationutils.rejectifemptyorwhitespace(result, "name", "field.required", "tag name required"); if (result.haserrors()) { return new modelandview("tag.edit", result.getmodel()); } tagdao.merge(tag); return new modelandview("redirect:/tags/listtags.htm"); }
Comments
Post a Comment