thymeleaf-spring icon indicating copy to clipboard operation
thymeleaf-spring copied to clipboard

Model from th:object is not found after AJAX Update

Open THD-Thomas-Lang opened this issue 5 years ago • 2 comments

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 InvoiceItem to 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

THD-Thomas-Lang avatar Jul 02 '20 12:07 THD-Thomas-Lang

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.

ultraq avatar Jul 03 '20 11:07 ultraq

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.

xtianus avatar Oct 12 '20 16:10 xtianus