spring - Fields error and globalerrors stay empty in Thymeleaf -
i using following code in project, issue have when there errors in bindingresult (bindingresult.haserrors() true), it's rendered false in thymeleaf result. makes me think bindingresult isn't "injected" correctly. did wrong in following code?
<form action="blog.html" th:action="@{/fileupload}" method="post" enctype="multipart/form-data" th:object="${form}"> <input type="text" name="title" th:field="*{title}" /> <input type="text" name="content" th:field="*{content}" /> <input type="file" name="myfile" th:field="*{myfile}" /> <input type="submit" /> <div id="errors" class="alert alert-error"> <ul th:if="${#fields.haserrors('*')}"> <li th:each="err : ${#fields.errors('*')}" th:text="${err}"></li> </ul> <div th:if="${#fields.hasglobalerrors()}"> <p th:each="err : ${#fields.globalerrors()}" th:text="${err}">...</p> </div> </div> </form>
controller
@requestmapping(value = "/blog", method = requestmethod.get) public string getindex(model model) { model.addattribute("form", new addblogform()); return "blog"; } @requestmapping(value = "/fileupload", method = requestmethod.post) public string importparse(model model, @valid addblogform form, bindingresult bindingresult) { model.addattribute("form", form); try { if (!bindingresult.haserrors()) { model.addattribute("successmessage", "succesfully added"); blogsrv.addpost(form.gettitle(), form.getcontent(), form.getmyfile()); model.addattribute("form", new addblogform()); } return "blog"; } catch (illegalstateexception e) { bindingresult.adderror(new objecterror("image", "illegalstateexception occured " + e.getmessage())); return "blog"; } catch (ioexception e) { bindingresult.adderror(new objecterror("image", "ioexception occured " + e.getmessage())); return "blog"; } }
you trying work around springs data binding, work framework.
first remove model
attribute method signature, second use redirect after successful save, , add @modelattribute
addblogform
annotation.
@requestmapping(value = "/fileupload", method = requestmethod.post) public string importparse(@valid @modelattribute addblogform form, bindingresult bindingresult) { try { if (!bindingresult.haserrors()) { blogsrv.addpost(form.gettitle(), form.getcontent(), form.getmyfile()); return "redirect:/blog"; } } catch (illegalstateexception e) { bindingresult.adderror(new objecterror("image", "illegalstateexception occured " + e.getmessage())); } catch (ioexception e) { bindingresult.adderror(new objecterror("image", "ioexception occured " + e.getmessage())); } return "blog" }
if want add success message model, use redirectattributes
instead , use flash message available after redirect.
@requestmapping(value = "/fileupload", method = requestmethod.post) public string importparse(@valid @modelattribute addblogform form, bindingresult bindingresult, redirectattributes attrs) { try { if (!bindingresult.haserrors()) { atts.setflashattribute("successmessage", "succesfully added"); blogsrv.addpost(form.gettitle(), form.getcontent(), form.getmyfile()); return "redirect:/blog"; } } catch (illegalstateexception e) { bindingresult.adderror(new objecterror("image", "illegalstateexception occured " + e.getmessage())); } catch (ioexception e) { bindingresult.adderror(new objecterror("image", "ioexception occured " + e.getmessage())); } return "blog" }
Comments
Post a Comment