thymeleaf / thymeleaf-spring

Thymeleaf integration module for Spring
http://www.thymeleaf.org
Apache License 2.0
435 stars 157 forks source link

Model from th:object is not found after AJAX Update #237

Open THD-Thomas-Lang opened 4 years ago

THD-Thomas-Lang commented 4 years ago

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)):

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

ultraq commented 4 years ago

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.

xtianus commented 4 years ago

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.