thymeleaf-spring
thymeleaf-spring copied to clipboard
Model from th:object is not found after AJAX Update
I use a model container Invoice with a given name and a list of given InvoiceItems to be rendered to html with thymleaf.
I am binding the model to th:field within my form.
Everything works fine.
No i want to add InvoiceItems to the Invoice container dynamically without refreshing the whole page (because in the real world my invoice object is very rich (beside this items)):
- Serialize the whole form model (for simplicity) with jquery
- Post this to a controller method with ajax
- Add one new
InvoiceItemto the list of those - Let thymeleaf render a partial view
After that i get this exception: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'items[0]' available as request attribute
When i debug the application i can see the right model values and even the right amount of items.
Please note I: I have the same issue with JSP, so maybe this is a spring issue. But maybe you can help me with that though.
Please note II: My workaround for this issue is to use a dedicated view for that. It is basically the same view as originally but with the model´s name prefixed to the th:fields. Strange, but it works.
I have already done a stackoverflow question - but without success: https://stackoverflow.com/questions/41242507/spring-modelattribute-not-recognized-anymore-after-ajax-update
Repository to comprehend this issue https://mygit.th-deg.de/tlang/thymefail
Moving to the thymeleaf-spring repo as I suspect the issue is closer to the Spring side than just Thymeleaf - a search for "Neither BindingResult nor plain target object" results in lots of posts about Spring MVC, regardless of templating technology used.
I don't think this is a bug neither in Thymeleaf nor Spring.
When you return a fragment that doesn't have a th:object, the *{} notation can't be used.